Hibernate SessionFactory vs. JPA EntityManagerFactory

elpisu picture elpisu · Apr 12, 2011 · Viewed 131.7k times · Source

I am new to Hibernate and I'm not sure whether to use a Hibernate SessionFactory or a JPA EntityManagerFactory to create a Hibernate Session.

What is the difference between these two? What are the pros & cons of using each of those?

Answer

Bozho picture Bozho · Apr 12, 2011

Prefer EntityManagerFactory and EntityManager. They are defined by the JPA standard.

SessionFactory and Session are hibernate-specific. The EntityManager invokes the hibernate session under the hood. And if you need some specific features that are not available in the EntityManager, you can obtain the session by calling:

Session session = entityManager.unwrap(Session.class);