java Hibernate Session 和 EntityManager 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16670447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between Hibernate Session and EntityManager
提问by JRR
What is the difference between the Hibernate Session
class and the EntityManager
class? I know that EntityManager
implements the Java Persistence API, but I am not sure what its relation is with Session
. Are they related at all?
HibernateSession
类和EntityManager
类有什么区别?我知道它EntityManager
实现了 Java Persistence API,但我不确定它与Session
. 他们有关系吗?
回答by EmirCalabuch
Session
is a hibernate-specific API, EntityManager
is a standardized API for JPA. You can think of the EntityManager
as an adapter class that wraps Session
(you can even get the Session
object from an EntityManager
object via the getDelegate()
function).
Session
是特定于休眠的 API,EntityManager
是 JPA 的标准化 API。您可以将EntityManager
视为包装的适配器类Session
(您甚至可以通过函数Session
从EntityManager
对象中获取对象getDelegate()
)。
This is not dissimilar to other Java APIs around (for example, JDBC is a standard API, each vendor adapts its product to the API via a driver that implements the standard functions).
这与周围的其他 Java API 没有什么不同(例如,JDBC 是一个标准 API,每个供应商都通过实现标准功能的驱动程序将其产品适应 API)。
回答by Sark
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:
SessionFactory 和 Session 是特定于休眠的。EntityManager 在后台调用休眠会话。如果您需要某些 EntityManager 中没有的特定功能,您可以通过调用获取会话:
Session session = entityManager.unwrap(Session.class);