Table of Contents
There are two APIs you can use with your mappings. One is the Hibernate Session and the other one the Java Persistence EntityManager. The latter is standardised as Java Persistence which is the replacement for Entity Beans in EJB 3.
With Java Persistence (JPA) 2.1 the API is good enough to be used instead of the Hibernate Session. Some people argue that it is better to use the Entity Manager as it is already included in any JEE application server like Glassfish, JBoss etc. and it allows to swap the Java Persistence implementation. Well, in theory this is true. In practice you will use implementation specific optimisations for data loading, caching and tuning. In fact, it is easier to deploy a Hibernate application on a different application server.
I prefer to use Hibernate for most cases as deployment on different application server or servlet engines is easier and I consider the criteria queries of JPA to be very hard to use as compared to Hibernate criteria queries. But there is an exception as well. If JEE with Session Beans is used as business layer technology, it is very easy to use Java Persistence. It is already provided. If in addition, the team has JPA experience, then I tend to prefer the JPA API.
The EntityManager of Hibernate is based on the Hibernate session. Basically, the EntityManager wraps a Hibernate session and adds some behaviour. The following code, shows how to get the underlying Hibernate session to get access to additional API methods like session.update.
Session hibernateSession = entityManager.unwrap(Session.class); hibernateSession.update(country);
I will introduce the Hibernate API first. You will find the EntityManager API in the next chapter.