Hibernate SessionFactory 与 JPA EntityManagerFactory
我是 Hibernate 新手,不确定是使用 Hibernate SessionFactory
还是 JPA EntityManagerFactory
来创建 Hibernate Session
。
这两者有什么区别?有什么优点和优点?使用其中每一个的缺点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
首选 EntityManagerFactory 和 EntityManager。它们由 JPA 标准定义。
SessionFactory
和Session
是特定于 hibernate 的。EntityManager
在底层调用 hibernate 会话。如果您需要 EntityManager 中不可用的某些特定功能,您可以通过调用以下方式获取会话:Prefer
EntityManagerFactory
andEntityManager
. They are defined by the JPA standard.SessionFactory
andSession
are hibernate-specific. TheEntityManager
invokes the hibernate session under the hood. And if you need some specific features that are not available in theEntityManager
, you can obtain the session by calling:SessionFactory
与EntityManagerFactory
正如我在 Hibernate 用户指南,Hibernate
SessionFactory
扩展了 JPAEntityManagerFactory
,如下图所示:因此,
SessionFactory
也是一个 JPAEntityManagerFactory
。SessionFactory
和EntityManagerFactory
都包含实体映射元数据,并允许您创建 HibernateSession
或EntityManager
。Session
与EntityManager
就像
SessionFactory
和EntityManagerFactory
一样,HibernateSession
扩展了JPA EntityManager。因此,EntityManager
定义的所有方法都可以在 HibernateSession
中使用。Session
和 `EntityManager 翻译 实体状态转换为 SQL 语句,例如 SELECT、INSERT、UPDATE 和 DELETE。Hibernate 与 JPA 引导
当引导 JPA 或 Hibernate 应用程序时,您有两种选择:
LocalSessionFactoryBean
完成的,如 此 GitHub 示例。Persistence
类或EntityManagerFactoryBuilder
。如果您使用 Spring,则 JPA 引导是通过 LocalContainerEntityManagerFactoryBean 完成的,如 此 GitHub 示例。通过 JPA 引导是首选。这是因为 JPA
FlushModeType.AUTO
是比旧版FlushMode.AUTO
更好的选择,后者破坏本机 SQL 查询的读写一致性。将 JPA 解包到 Hibernate
另外,如果您通过 JPA 引导,并且已通过 @PersistenceUnit 注释注入了 EntityManagerFactory:
您可以轻松访问底层 Sessionfactory 使用
unwrap
方法:使用 JPA
EntityManager
也可以完成相同的操作。如果您通过@PersistenceContext
注释注入EntityManager
:您可以使用
unwrap
轻松访问底层Session
> 方法:结论
因此,您应该通过 JPA 引导,使用 EntityManagerFactory 和 EntityManager,并且仅当您想要访问某些 Hibernate 时才将它们解包到其关联的 Hibernate 接口- JPA 中不可用的特定方法,例如通过其 自然标识符。
SessionFactory
vs.EntityManagerFactory
As I explained in the Hibernate User Guide, the Hibernate
SessionFactory
extends the JPAEntityManagerFactory
, as illustrated by the following diagram:So, the
SessionFactory
is also a JPAEntityManagerFactory
.Both the
SessionFactory
and theEntityManagerFactory
contain the entity mapping metadata and allow you to create a HibernateSession
or aEntityManager
.Session
vs.EntityManager
Just like the
SessionFactory
andEntityManagerFactory
, the HibernateSession
extends the JPAEntityManager
. So, all methods defined by theEntityManager
are available in the HibernateSession
.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:
SessionFactory
via theBootstrapServiceRegistryBuilder
. If you're using Spring, the Hibernate bootstrap is done via theLocalSessionFactoryBean
, as illustrated by this GitHub example.EntityManagerFactory
via thePersistence
class or theEntityManagerFactoryBuilder
. If you're using Spring, the JPA bootstrap is done via theLocalContainerEntityManagerFactoryBean
, 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 legacyFlushMode.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:You can easily get access to the underlying
Sessionfactory
using theunwrap
method:The same can be done with the JPA
EntityManager
. If you inject theEntityManager
via the@PersistenceContext
annotation:You can easily get access to the underlying
Session
using theunwrap
method:Conclusion
So, you should bootstrap via JPA, use the
EntityManagerFactory
andEntityManager
, 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.我想补充一点,您还可以通过从
EntityManager
调用getDelegate()
方法来获取 Hibernate 的会话。前任:
I want to add on this that you can also get Hibernate's session by calling
getDelegate()
method fromEntityManager
.ex:
与
SessionFactory
相比,我更喜欢 JPA2EntityManager
API,因为它感觉更现代。一个简单的例子:JPA:
SessionFactory:
我认为很明显,第一个看起来更干净,也更容易测试,因为 EntityManager 可以很容易地被模拟。
I prefer the JPA2
EntityManager
API overSessionFactory
, because it feels more modern. One simple example:JPA:
SessionFactory:
I think it's clear that the first one looks cleaner and is also easier to test because EntityManager can be easily mocked.
使用 EntityManagerFactory 方法允许我们使用 @PrePersist、@PostPersist、@PreUpdate 等回调方法注释,而无需额外配置。
在使用 SessionFactory 时使用类似的回调将需要额外的努力。
相关 Hibernate 文档可以在此处 和 这里。
相关 SOF 问题和Spring 论坛讨论
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
EntityManagerFactory 是标准实现,所有实现都是相同的。如果您将 ORM 迁移到任何其他提供商(例如 EclipseLink),则处理事务的方法不会有任何变化。相反,如果您使用 hibernate 的会话工厂,它会与 hibernate API 绑定并且无法迁移到新的供应商。
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.
通过使用 EntityManager,代码不再与 hibernate 紧密耦合。但为此,在使用中我们应该使用 :
而不是
类似地,对于 EntityManagerFactory,使用 javax 接口。这样,代码就松耦合了。如果有比 hibernate 更好的 JPA 2 实现,那么切换就会很容易。在极端情况下,我们可以将类型转换为 HibernateEntityManager。
By using EntityManager, code is no longer tightly coupled with hibernate. But for this, in usage we should use :
instead of
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.
EntityManager接口类似于hibernate中的sessionFactory。
EntityManager 在 javax.persistance 包下,而 session 和 sessionFactory 在 org.hibernate.Session/sessionFactory 包下。
实体管理器是 JPA 特定的,而 session/sessionFactory 是 hibernate 特定的。
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.