简体   繁体   中英

Are Session and EntityManager the same?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM