java 使用 Hibernate EntityManager(与 Hibernate Core)有什么缺点吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3440532/
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
Any drawbacks of using Hibernate EntityManager (vs. Hibernate Core)?
提问by Chris Lercher
The Hibernate EntityManager documentation states, that:
Hibernate EntityManager 文档指出:
You may use a combination of all three together, annotations without JPA programming interfaces and lifecycle, or even pure native Hibernate Core, depending on the business and technical needs of your project. You can at all times fall back to Hibernate native APIs, or if required, even to native JDBC and SQL.
根据项目的业务和技术需求,您可以将这三者结合使用,没有 JPA 编程接口和生命周期的注解,甚至纯原生 Hibernate Core。您可以随时回退到 Hibernate 本机 API,或者如果需要,甚至回退到本机 JDBC 和 SQL。
Code that uses the JPA API (EntityManager) is clearly more portable (even with occasional fallbacks to Hibernate Core).
使用 JPA API (EntityManager) 的代码显然更具可移植性(即使偶尔回退到 Hibernate Core)。
But would I have any advantages when using purely Hibernate Core? I wonder, if the JPA 2 model really fits on top of Hibernate Core without any contradictions? IOW, is a fallback to Core always easy and without problems?
但是在使用纯粹的 Hibernate Core 时我会有什么优势吗?我想知道,JPA 2 模型是否真的适合在 Hibernate Core 之上而没有任何矛盾?IOW,回退到 Core 总是容易且没有问题吗?
My main concern is this:
我主要关心的是:
Maybe the differences are not only in the API, but also in the underlying semantics?! (e. g. different transaction/versioning/locking semantics that may conflict: Pessimistic locking is mentioned in the Core documentation, but not in the EntityManager documentation - so could I still use pessimistic locking by falling back to Core without causing problems? Things like that...)
也许差异不仅在于 API,还在于底层语义?!(例如,可能发生冲突的不同事务/版本控制/锁定语义:Core 文档中提到了悲观锁定,但 EntityManager 文档中没有提到 - 所以我仍然可以通过回退到 Core 来使用悲观锁定而不引起问题吗?诸如此类的...... .)
采纳答案by Pascal Thivent
But would I have any advantages when using purely Hibernate Core?
但是在使用纯粹的 Hibernate Core 时我会有什么优势吗?
If JPA 2.0 supports what you need, there is in my opinion no advantage at using Hibernate Core directly (and with JPA 2.0, the gap became more thin, making the need to fallback to Core the exception, not the rule, which is a very good thing).
如果 JPA 2.0 支持你所需要的,在我看来直接使用 Hibernate Core 没有任何优势(并且使用 JPA 2.0,差距变得更小,使得需要回退到 Core 成为例外,而不是规则,这是一个非常好东西)。
I wonder, if the JPA 2 model really fits on top of Hibernate Core without any contradictions?
我想知道,JPA 2 模型是否真的适合在 Hibernate Core 之上而没有任何矛盾?
It does since JPA 1.0, the Hibernate developers created Hibernate3 with "JPA in mind" and adopted JPA semantics, defaults, etc in Hibernate3. You might want to listen to Gavin in this Tech Talk: Gavin King on Hibernate3 and EJB3:
从 JPA 1.0 开始,Hibernate 开发人员就以“考虑到 JPA”的方式创建了 Hibernate3,并在 Hibernate3 中采用了 JPA 语义、默认值等。您可能想在此技术讲座中聆听 Gavin 的 演讲:Gavin King on Hibernate3 and EJB3:
In this tech talk King discusses how Hibernate3 builds upon and extends EJB3, addressing such topics as:
- New features of Hibernate3
- The relationship between Hibernate3 and the EJB3 container in JBoss
- What differentiates Hibernate3 from the EJB3 specification
- Hibernate's custom annotations available outside EJB
- The future of Hibernate
在本次技术演讲中,King 讨论了 Hibernate3 如何构建和扩展 EJB3,讨论的主题包括:
- Hibernate3 的新特性
- JBoss中Hibernate3和EJB3容器的关系
- Hibernate3 与 EJB3 规范的区别
- Hibernate 的自定义注解在 EJB 之外可用
- Hibernate的未来
And according to my practical experience, the fact that Hibernate doesn't contradict with EJB 3 is true.
而且根据我的实践经验,Hibernate 与 EJB 3 并不矛盾这一事实是正确的。
IOW, is a fallback to Core always easy and without problems?
IOW,回退到 Core 总是容易且没有问题吗?
Whether you use Core directly or not, you areusing it (the EntityManageris a wrapper around the Session). So, yes, falling back to Core is easy if you really have to (for things that are still not in the spec like Query By Example, for example). And, no, this won't cause any problems (since you actually are using it, or a subset of it, in JPA).
无论您是否直接使用 Core,您都在使用它(这EntityManager是围绕 的包装器Session)。所以,是的,如果你真的必须回到 Core 是很容易的(例如,对于那些仍然不在规范中的东西,比如 Query By Example)。而且,不,这不会导致任何问题(因为您实际上是在 JPA 中使用它或它的一个子集)。
Related questions
相关问题
回答by Arthur Ronald
It has been clear: depending on the business and technical needs of your project
已经很清楚了:取决于您项目的业务和技术需求
But would I have any advantages when using purely Hibernate Core ?
但是在使用纯粹的 Hibernate Core 时我会有什么优势吗?
Do not forget both Hibernate Annotations and Hibernate EntityManager is built on top of Hibernate core. So one more layer. Besides That, it rely on its mapping metadata stored in XML files. Some users prefer To use XML files instead of annotations because it leaves the domain objects completely independent of the DAO implementation you choose. But when using annotations, Hibernate with JPA book is clear
不要忘记 Hibernate Annotations 和 Hibernate EntityManager 都是建立在 Hibernate 核心之上的。所以多一层。除此之外,它依赖于存储在 XML 文件中的映射元数据。一些用户更喜欢使用 XML 文件而不是注释,因为它使域对象完全独立于您选择的 DAO 实现。但是当使用注解时,Hibernate with JPA book 是清楚的
reduce your lines of code for mapping metadata, compared to the native XML files, and you may like the better refactoring capabilities of annotations
与原生 XML 文件相比,减少用于映射元数据的代码行,并且您可能会喜欢注释更好的重构功能
JDBC >> Hibernate core >> Hibernate Annotations
...
...
JDBC >> Hibernate core >> Hibernate EntityManager
And JPA 2 fits better what JPA 1 has not been provided. A lot of new features is now available such as
而 JPA 2 更适合 JPA 1 未提供的内容。现在可以使用许多新功能,例如
- Object oriented query API
- Wrapper's @Embeddable
- Collection of @Embeddables @ElementCollection
- 面向对象的查询 API
- 包装器的@Embeddable
- @Embeddables @ElementCollection 的集合
And so on...
等等...
Do not forget JPA EntityManager allows you retrieve its underlying vendor-specific provider by using getDelegatemethod if you need features where JPA does not provide.
如果您需要 JPA 未提供的功能,请不要忘记 JPA EntityManager 允许您使用getDelegate方法检索其底层供应商特定提供程序。
回答by Thomas W
I like using Hibernate Core directly. I also prefer XML mapping files.
我喜欢直接使用 Hibernate Core。我也更喜欢 XML 映射文件。
I'm not really interested in using a secondary layer, to access Hibernate core functionality. As far as I am concerned portability in the persistence tier, is a complete non-issue.
我对使用辅助层来访问 Hibernate 核心功能并不真正感兴趣。就我而言,持久层的可移植性完全不是问题。
There are very few real advantages of the JPA API over Hibernate API -- I saw people using JPA named queries (where Criteria would have probably been clearer & better), and the JPA annotations are somewhat better designed.
JPA API 相对于 Hibernate API 的真正优势很少——我看到人们使用 JPA 命名查询(其中 Criteria 可能更清晰和更好),并且 JPA 注释设计得更好一些。
Other than that, JPA is just a layer adding complexity & potential overhead -- for no benefit. Architecturally in such a situation, the correct decision would be against using it.
除此之外,JPA 只是一个增加复杂性和潜在开销的层——没有任何好处。从架构上讲,在这种情况下,正确的决定是反对使用它。
Database portability OTOH is very real. I have custom allocators (generators) I use which are completely portable, and far more performant & simpler than the crazy mess which is the mis-designed Hibernate ones.
数据库可移植性 OTOH 是非常真实的。我有我使用的自定义分配器(生成器),它们是完全可移植的,并且比设计错误的 Hibernate 的疯狂混乱性能更高且更简单。
This approach has been very successful on multiple major commercial, Government & legacy database re-engineering projects. In short -- focus on what's important -- and an API on top of an API, is not it.
这种方法在多个主要的商业、政府和遗留数据库重新设计项目中非常成功。简而言之——专注于重要的事情——以及在 API 之上的 API,不是吗。
回答by Jacob
The benefits of using the JPA api far outweigh any disadvantages of using pure hibernate. I am not an expert but I am not aware of any situations where it would be beneficial to to drop down to hibernate. (Dropping down to do pure JDBC is a different matter.)
使用 JPA api 的好处远远超过使用纯休眠的任何缺点。我不是专家,但我不知道在任何情况下进入休眠状态是有益的。(下拉做纯 JDBC 是另一回事。)
If anyone has any examples to share I would love to see them.
如果有人有任何示例可以分享,我很乐意看到它们。

