vicky vicky - 1 year ago 65
Java Question

Hibernate SessionFactory vs EntityManagerFactory

I have been moving around with this question and the bird eye difference between Hibernate SessionFactory and JPA EntityManagerFactory is that JPA is standard. You can use it without being afraid of underlying ORM. yet it calls the underlying sessionFactory under the hood.(Correct me if i am wrong)

But if someone knows that he has just to stick with hibernate as a ORM in the future, then what should he choose from these two Factories and why?

Secondly, what are the other differences between these two with respect to performance, features, stability etc?

Answer Source

You should prefer the standard JPA API over the proprietary Hibernate one, for several reasons:

  1. It makes you learn something that you can reuse in more other projects, relying on a different implementation
  2. The JPA API is cleaner than the Hibernate one: it doesn't have the early mistakes that the Hibernate API has
  3. The efforts and evolutions are now targeted at the JPA API. For example, the standard JPA2 criteria API is more complete than the old, proprietary, Hibernate Criteria API (but more complex to use, IMHO)
  4. If you want, you can always get the Hibernate Session from the JPA EntityManager. Not vice-versa

Anyway, most of the effort is in mapping the entities themselves, and that is done using standard JPA annotations, even when using the Session API.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download