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!
There might be some more slight differences between those 2 but the main concept is the following:
Sessionbelongs toorg.hibernatepackage, and is an interface ofHibernate. This belongs to a specific library that provides implementation of JPA, namelyhibernate.EntityManagerbelongs tojavax.persistencewhich belongs to Java EE and not some external library. This is the package that the specifications forJPAare provided fromoracle. Every library that wants to implementJPAin java ecosystem must follow these specifications from this package.If you have
Hibernateconfigured in your project, then for example theentityManager.persist(obj)will invoke thesession.persist(obj)through the delegate architecture that hibernate has and then thesessionImpl.persist(obj)of hibernate will be invoked which will do the actual job.To sum up:
sessionis just an API forHibernateonly.EntityManageris an API provided by Java EE forJPA.JPA is also implemented by another library
eclipselink. In the future a requirement may arise and you may need to switch to useeclipselinklibrary instead ofhibernate. This is why you should construct your project to be based onentityManagerinstead of some library specific API.