Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate SessionFactory vs EntityManagerFactory [duplicate]

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?

like image 973
vicky Avatar asked May 03 '14 14:05

vicky


People also ask

Is SessionFactory a JPA entitymanagerfactory?

So, the SessionFactory is also a JPA EntityManagerFactory. Both the SessionFactory and the EntityManagerFactory contain the entity mapping metadata and allow you to create a Hibernate Session or a EntityManager. Just like the SessionFactory and EntityManagerFactory, the Hibernate Session extends the JPA EntityManager.

What is the difference between hibernate and EntityManager session factory?

In contrast, if you use hibernate’s session factory, it is tied to hibernate APIs and ca not migrate to new vendor easily. One dis-advantage of using the standard implementation is that, it is not providing the advanced features. There is not much control provided in the EntityManager APIs.

What is the difference between session and EntityManager in Salesforce?

Regarding differences between Session and EntityManager methods, we should note that Session has more methods to analyze its internal state. The single common method is called isOpen and permits to check if Session or EntityManager is open.

How to create a session in hibernate?

It can be created through providing objects of Configuration. This will contain all the database property details which are pulled from either hibernate.properties file or hibernate.cfg.xml file. Session objects consist and are like a factory in SessionFactory.


1 Answers

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.

like image 167
JB Nizet Avatar answered Sep 26 '22 15:09

JB Nizet