Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use an entity manager?

I am using Hibernate to map objects to entities and I have started to use an Entity Manager. This might be a silly question but what actually is the reason for using the entity manager? Previously I would have used a HibernateSessionFactory to get a session and then use that session to pull/push data.

like image 610
christophmccann Avatar asked Sep 17 '25 20:09

christophmccann


2 Answers

Because the EntityManager is part of the standard - JPA. Theoretically, you can switch implementations (Hibernate, EclipseLink, OpenJPA) if you need to. Apart from alleged portability there isn't such a big difference between the two.

Hibernate implements the JPA standard. In fact, the EntityManager has a delegate, based on the concrete implementation. For Hibernate the delegate is the Session. If you call getDelegate() it will return the current Session.

I've always used hibernate with JPA (EntityManager) and had very rarely had the need to obtain the Session.

like image 156
Bozho Avatar answered Sep 20 '25 10:09

Bozho


EntityManager is a concept of JPA. You dont need to use JPA with Hibernate at all (in fact, if it's JPA1, I would suggest you dont).

like image 39
Steven Avatar answered Sep 20 '25 09:09

Steven