Home > OS >  Are Session and EntityManager the same?
Are Session and EntityManager the same?

Time:08-06

New to JPA and was wondering if Session (Hibernate) and EntityManager are the same thing or not? As both of them basically allow you to manage and search for entities in the relational database.

Also any good source materials on the 2 would be helpful. Thank you in advance!

CodePudding user response:

There might be some more slight differences between those 2 but the main concept is the following:

  • Session belongs to org.hibernate package, and is an interface of Hibernate. Meaning it belongs to the specific JPA vendor of hibernate.
  • EntityManager belongs to javax.persistence which belongs to Java EE and not some external library. This is the package that the specifications for JPA are provided from oracle. Every library that wants to implement JPA in java ecosystem must follow these specifications from this package.

If you have Hibernate configured in your project, then for example the entityManager.persist(obj) will invoke the session.persist(obj) through the delegate architecture that hibernate has and then the sessionImpl.persist(obj) of hibernate will be invoked which will do the actual job.

To sum up:

  • session is just an API for Hibernate only.
  • EntityManager is an API provided by Java EE for JPA.

JPA is also implemented by another library eclipselink. In the future a requirement may arise and you may need to switch to use eclipselink library instead of hibernate. This is why you should construct your project to be based on entityManager instead of some library specific API.

CodePudding user response:

Hibernate first appeared in 2001. Among the object-relational mapping frameworks that emerged in Java, it was probably the most famous, complete, and successful. It uses Session.

However, Hibernate is a specific framework developed by a specific group. In 2005, several groups came together under the coordination of Sun to develop and establish an official standard to define in generic terms the behavior of object-relational mapping frameworks. Furthermore, it was defined that this framework would use the new features of the Java 5 language (released in 2004), since no mature object-relational framework used them (generic types and annotations, mainly). The works were heavily inspired by Hibernate and there was a lot of participation from the Hibernate people. From this, JPA was born in 2006. JPA uses EntityManager.

Another goal to be achieved with the creation of the JPA was the elimination of BMP and CMP from EJB 2 (will not be missed). Basically CMP was an attempt to create a persistence specification and object-relational mapping, but it was quite complicated, difficult to use, tightly coupled to EJB 2 and offered far fewer features than Hibernate. BMP was something closer to what entity beans (annotated with @Entity) are used by JPA today, although they were in a much less developed state. But that is already a subject for another topic.

When JPA was finally born, Hibernate obviously started to implement JPA, thereby gaining adherence to the official standard and also the use of generic types and annotations. That is, he came to recognize everything that the JPA recognizes. In early versions of Hibernate with JPA support, the EntityManager redirected everything it did to the Session. In newer versions of Hibernate (5.2 ), this is even simpler because the Session interface extends the EntityManager interface directly.

However, Hibernate is just one of the existing implementations of JPA (although it is a very good one and is the most successful one). Other implementations of JPA have emerged

  • Related