christophmccann
christophmccann

Reputation: 4211

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.

Upvotes: 4

Views: 3681

Answers (3)

gpeche
gpeche

Reputation: 22524

You use an EntityManager when you are using JPA API. Hibernate implementation of EntityManager internally calls HibernateSessionFactory and manages Hibernate sessions for you.

EntityManagers in JPA serve basically the same purpose as Hibernate sessions.

Upvotes: 1

Bozho
Bozho

Reputation: 597422

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.

Upvotes: 6

Steven
Steven

Reputation: 3894

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).

Upvotes: 2

Related Questions