Hibernate SessionFactory vs. JPA EntityManagerFactory

后端 未结 8 2204
清歌不尽
清歌不尽 2020-11-27 09:03

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

相关标签:
8条回答
  • 2020-11-27 09:20

    EntityManagerFactory is the standard implementation, it is the same across all the implementations. If you migrate your ORM for any other provider like EclipseLink, there will not be any change in the approach for handling the transaction. In contrast, if you use hibernate’s session factory, it is tied to hibernate APIs and cannot migrate to new vendor.

    0 讨论(0)
  • 2020-11-27 09:21

    SessionFactory vs. EntityManagerFactory

    As I explained in the Hibernate User Guide, the Hibernate SessionFactory extends the JPA EntityManagerFactory, as illustrated by the following diagram:

    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.

    Session vs. EntityManager

    Just like the SessionFactory and EntityManagerFactory, the Hibernate Session extends the JPA EntityManager. So, all methods defined by the EntityManager are available in the Hibernate Session.

    The Session and the `EntityManager translate entity state transitions into SQL statements, like SELECT, INSERT, UPDATE, and DELETE.

    Hibernate vs. JPA bootstrap

    When bootstrapping a JPA or Hibernate application, you have two choices:

    1. You can bootstrap via the Hibernate native mechanism, and create a SessionFactory via the BootstrapServiceRegistryBuilder. If you're using Spring, the Hibernate bootstrap is done via the LocalSessionFactoryBean, as illustrated by this GitHub example.
    2. Or, you can create a JPA EntityManagerFactory via the Persistence class or the EntityManagerFactoryBuilder. If you're using Spring, the JPA bootstrap is done via the LocalContainerEntityManagerFactoryBean, as illustrated by this GitHub example.

    Bootstrapping via JPA is to be preferred. That's because the JPA FlushModeType.AUTO is a much better choice than the legacy FlushMode.AUTO, which breaks read-your-writes consistency for native SQL queries.

    Unwrapping JPA to Hibernate

    Also, if you bootstrap via JPA, and you have injected the EntityManagerFactory via the @PersistenceUnit annotation:

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    

    You can easily get access to the underlying Sessionfactory using the unwrap method:

    SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
    

    The same can be done with the JPA EntityManager. If you inject the EntityManager via the @PersistenceContext annotation:

    @PersistenceContext
    private EntityManager entityManager;
    

    You can easily get access to the underlying Session using the unwrap method:

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

    Conclusion

    So, you should bootstrap via JPA, use the EntityManagerFactory and EntityManager, and only unwrap those to their associated Hibernate interfaces when you want to get access to some Hibernate-specific methods that are not available in JPA, like fetching the entity via its natural identifier.

    0 讨论(0)
  • 2020-11-27 09:29

    Using EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist,@PreUpdate with no extra configuration.

    Using similar callbacks while using SessionFactory will require extra efforts.

    Related Hibernate docs can be found here and here.

    Related SOF Question and Spring Forum discussion

    0 讨论(0)
  • 2020-11-27 09:35

    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);
    
    0 讨论(0)
  • 2020-11-27 09:36

    By using EntityManager, code is no longer tightly coupled with hibernate. But for this, in usage we should use :

    javax.persistence.EntityManager
    

    instead of

    org.hibernate.ejb.HibernateEntityManager
    

    Similarly, for EntityManagerFactory, use javax interface. That way, the code is loosely coupled. If there is a better JPA 2 implementation than hibernate, switching would be easy. In extreme case, we could type cast to HibernateEntityManager.

    0 讨论(0)
  • 2020-11-27 09:36

    EntityManager interface is similar to sessionFactory in hibernate. EntityManager under javax.persistance package but session and sessionFactory under org.hibernate.Session/sessionFactory package.

    Entity manager is JPA specific and session/sessionFactory are hibernate specific.

    0 讨论(0)
提交回复
热议问题