Java Hibernate SessionFactory 与 EntityManagerFactory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23445830/
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
Hibernate SessionFactory vs EntityManagerFactory
提问by vicky
I have been moving around with this question and the bird eye difference between Hibernate SessionFactoryand JPA EntityManagerFactoryis that JPA is standard. You can use it without being afraid of underlying ORM. yet it calls the underlying sessionFactory under the hood.(Correct me if i am wrong)
我一直在讨论这个问题,Hibernate SessionFactory和JPA EntityManagerFactory之间的鸟瞰差异在于JPA 是标准的。您可以使用它而不必担心底层 ORM。但它在底层调用了底层 sessionFactory。(如果我错了,请纠正我)
But if someone knows that he has just to stick with hibernate as a ORM in the future, then what should he choose from these two Factoriesand why?
但是如果有人知道他以后只能坚持使用hibernate作为ORM,那么他应该从这两个Factories中选择什么,为什么?
Secondly, what are the other differences between these two with respect to performance, features, stability etc?
其次,这两者在性能、功能、稳定性等方面还有哪些其他区别?
采纳答案by JB Nizet
You should prefer the standard JPA API over the proprietary Hibernate one, for several reasons:
出于以下几个原因,您应该更喜欢标准的 JPA API 而不是专有的 Hibernate API:
- It makes you learn something that you can reuse in more other projects, relying on a different implementation
- The JPA API is cleaner than the Hibernate one: it doesn't have the early mistakes that the Hibernate API has
- The efforts and evolutions are now targeted at the JPA API. For example, the standard JPA2 criteria API is more complete than the old, proprietary, Hibernate Criteria API (but more complex to use, IMHO)
- If you want, you can always get the Hibernate Session from the JPA EntityManager. Not vice-versa
- 它让你学到一些可以在更多其他项目中重用的东西,依赖于不同的实现
- JPA API 比 Hibernate API 更干净:它没有 Hibernate API 的早期错误
- 这些努力和演变现在针对 JPA API。例如,标准 JPA2 标准 API 比旧的、专有的 Hibernate Criteria API 更完整(但使用起来更复杂,恕我直言)
- 如果需要,您始终可以从 JPA EntityManager 获取 Hibernate Session。反之亦然
Anyway, most of the effort is in mapping the entities themselves, and that is done using standard JPA annotations, even when using the Session API.
无论如何,大部分工作是映射实体本身,这是使用标准 JPA 注释完成的,即使在使用 Session API 时也是如此。