java jpa和hibernate的异同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1770211/
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
similarity and difference between jpa and hibernate
提问by Satya
what is similarity and difference between jpa and hibernate.
jpa 和 hibernate 之间的相似之处和区别是什么。
采纳答案by Jeff Foster
JPA (Java Persistence API) is an interface for persistence providers to implement. Hibernate is one such implementation of JPA.
JPA(Java Persistence API)是持久化提供者实现的接口。Hibernate 就是 JPA 的一种这样的实现。
回答by Pascal Thivent
This is the introduction of the JSR-000220 Enterprise JavaBeans 3.0 Final Release (persistence):
这是 JSR-000220 Enterprise JavaBeans 3.0 Final Release(持久性)的介绍:
This document is the specification of the Java API for the management of persistence and object/rela- tional mapping with Java EE and Java SE. The technical objective of this work is to provide an object/relational mapping facility for the Java application developer using a Java domain model to man- age a relational database.
This persistence API—together with the query language and object/relational mapping metadata defined in this document—is required to be supported under Enterprise JavaBeans 3.0. It is also targeted at being used stand-alone with Java SE.
Leading experts throughout the entire Java community have come together to build this Java persistence standard. This work incorporates contributions from the Hibernate, TopLink, and JDO communities, as well as from the EJB community.
本文档是 Java API 的规范,用于使用 Java EE 和 Java SE 管理持久性和对象/关系映射。这项工作的技术目标是为使用 Java 域模型管理关系数据库的 Java 应用程序开发人员提供对象/关系映射工具。
Enterprise JavaBeans 3.0 需要支持此持久性 API 以及本文档中定义的查询语言和对象/关系映射元数据。它还旨在与 Java SE 独立使用。
整个 Java 社区的领先专家齐聚一堂,共同构建了这个 Java 持久性标准。这项工作结合了 Hibernate、TopLink 和 JDO 社区以及 EJB 社区的贡献。
In other words, JPA is the standardizedAPI for persistence. Hibernate provides an implementation of the standard (i.e. it can be used as underlying persistence engine when using JPA).
换句话说,JPA 是用于持久性的标准化API。Hibernate 提供了该标准的实现(即,在使用 JPA 时,它可以用作底层持久性引擎)。
Prior to JPA, Hibernate was a kind of de facto standard for object persistence in Java. Hibernate is considered as a major contributor to JPA so there are lots of similarities between them (I could even say between all ORM solutions as ORM concepts are common). However, for various reasons, political or technical, the JPA working group couldn't include everything in the first release of JPA so JPA is actually a subset of Hibernate (this is a simplified view but the reality is very close). Hibernate and other JPA implementation provide thus their own proprietary extensions (which means non standards i.e. not portable from one provider to the other) to the JPA standard that you may use, or not.
在 JPA 之前,Hibernate 是 Java 中对象持久性的一种事实上的标准。Hibernate 被认为是 JPA 的主要贡献者,因此它们之间有很多相似之处(我什至可以说在所有 ORM 解决方案之间,因为 ORM 概念很常见)。但是,出于各种或技术原因,JPA 工作组无法在 JPA 的第一个版本中包含所有内容,因此 JPA 实际上是 Hibernate 的一个子集(这是一个简化的视图,但实际情况非常接近)。因此,Hibernate 和其他 JPA 实现为您可能使用或不使用的 JPA 标准提供了它们自己的专有扩展(这意味着非标准,即不能从一个提供者移植到另一个提供者)。
回答by non sequitor
As pointed out by @Pascal Hibernate existed prior to JPA standard(it is now JSR 317 JPA 2.0, which Hibernate has implemented in 3.5 already out for early use). So there are other providers of JPA e.g. Oracle TopLink, Apache OpenJPA. To use strictly JPA in Hibernate you have to use EntityManageras apposed to the Sessionwhich is a Hibernate concept pre-dating JPA. Similarly for strict JPA you have to use EntityManagerFactoryas apposed to SessionFactory.
正如@Pascal Hibernate 所指出的,早于 JPA 标准就已经存在(现在是 JSR 317 JPA 2.0,Hibernate 已经在 3.5 中实现了早期使用)。因此还有其他 JPA 提供者,例如 Oracle TopLink、Apache OpenJPA。为了在Hibernate中使用JPA严格,你必须使用EntityManager作为并列的Session是Hibernate的概念要早于JPA。同样,对于严格的JPA你必须使用EntityManagerFactory作为并列来SessionFactory。
The EntityManagerand EntityManagerFactoryare, in fact, thin wrappers around the Sessionand SessionFactoryrespectively. In addition, core/annotations Hibernate provide extensions to the JPA spec that make your life easier. Before JPA 2 Hibernate had a Criteria API which allowed you to programmatically construct a query in an OO fashion. JPA 2 now makes this functionality standard -- the JPA standard is a bit different from the Hibernate version as it employs generics. So basically the Hibernate functionality is a superset of JPA.
在EntityManager和EntityManagerFactory,实际上,围绕薄包装Session和SessionFactory分别。此外,Hibernate 的核心/注释提供 JPA 规范的扩展,使您的生活更轻松。在 JPA 2 Hibernate 之前,Hibernate 有一个 Criteria API,它允许您以面向对象的方式以编程方式构造查询。JPA 2 现在使这个功能成为标准——JPA 标准与 Hibernate 版本有点不同,因为它使用了泛型。所以基本上 Hibernate 功能是 JPA 的超集。
回答by Serge Seletskyy
Here's a list of some difference between subjects (excerpts from Hibernate documentation)
这是主题之间的一些差异列表(摘自 Hibernate 文档)
Entity. Hibernate goes beyond the JPA specification and provide additional configurations. Some of them are hosted on @org.hibernate.annotations.Entity
- dynamicInsert / dynamicUpdate (defaults to false)
- selectBeforeUpdate (defaults to false)
- polymorphisms
- persister
- optimisticLock (version, all, dirty, none)
id as a property using a component type
- While not supported in JPA, Hibernate lets you place your association directly in the embedded id component (instead of having to use the @MapsId annotation)
Multiple id properties without identifier type
- Another, arguably more natural, approach is to place @Id on multiple properties of your entity. This approach is only supported by Hibernate (not JPA compliant) but does not require an extra embeddable component.
Multiple id properties with with a dedicated identifier type
- While not JPA standard, Hibernate let's you declare the vanilla associated property in the @IdClass
Identifier generator
- Package level definition is not supported by the JPA specification. However, you can use the @GenericGenerator at the package level
Annotations
- Hibernate Annotations supports something that is not explicitly supported by the JPA specification. You can annotate a embedded object with the @MappedSuperclass annotation to make the superclass properties persistent (see @MappedSuperclass for more informations).
Discriminator
- @org.hibernate.annotations.DiscriminatorOptions allows to optionally specify Hibernate specific discriminator options which are not standardized in JPA. The available options are force and insert
Transaction
- Hibernate provides more flush modes than the one described in the JPA specification. In particularFlushMode.MANUAL for long running conversation. Please refer to the Hibernate core reference documentation for more informations.
Static metamodel
- Important. As of today the JPA 2 metamodel does not provide any facility for accessing relational information pertaining to the physical model. It is expected this will be addressed in a future release of the specification.
实体。Hibernate 超越了 JPA 规范并提供了额外的配置。其中一些托管在@org.hibernate.annotations.Entity
- dynamicInsert / dynamicUpdate(默认为false)
- selectBeforeUpdate(默认为 false)
- 多态性
- 坚持者
- 乐观锁(版本,全部,脏,无)
id 作为使用组件类型的属性
- 虽然 JPA 不支持,但 Hibernate 允许您将关联直接放在嵌入的 id 组件中(而不必使用 @MapsId 注释)
没有标识符类型的多个 id 属性
- 另一种可以说更自然的方法是将 @Id 放在实体的多个属性上。这种方法仅受 Hibernate 支持(不符合 JPA),但不需要额外的可嵌入组件。
具有专用标识符类型的多个 id 属性
- 虽然不是 JPA 标准,但 Hibernate 让你在 @IdClass 中声明 vanilla 关联属性
标识符生成器
- JPA 规范不支持包级别定义。但是,您可以在包级别使用@GenericGenerator
注释
- Hibernate Annotations 支持 JPA 规范未明确支持的内容。您可以使用 @MappedSuperclass 注释来注释嵌入的对象,以使超类属性持久化(有关更多信息,请参阅 @MappedSuperclass)。
鉴别器
- @org.hibernate.annotations.DiscriminatorOptions 允许选择性地指定在 JPA 中未标准化的 Hibernate 特定鉴别器选项。可用选项是强制和插入
交易
- Hibernate 提供了比 JPA 规范中描述的更多的刷新模式。特别是FlushMode.MANUAL 用于长时间运行的对话。有关更多信息,请参阅 Hibernate 核心参考文档。
静态元模型
- 重要的。截至今天,JPA 2 元模型不提供任何用于访问与物理模型相关的关系信息的工具。预计这将在规范的未来版本中得到解决。

