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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:36:55  来源:igfitidea点击:

difference between Hibernate Session and EntityManager

javahibernatejpa

提问by JRR

What is the difference between the Hibernate Sessionclass and the EntityManagerclass? I know that EntityManagerimplements 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

Sessionis a hibernate-specific API, EntityManageris a standardized API for JPA. You can think of the EntityManageras an adapter class that wraps Session(you can even get the Sessionobject from an EntityManagerobject via the getDelegate()function).

Session是特定于休眠的 API,EntityManager是 JPA 的标准化 API。您可以将EntityManager视为包装的适配器类Session(您甚至可以通过函数SessionEntityManager对象中获取对象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);