我知道JPA 2是一个规范,Hibernate是ORM的工具。另外,我知道Hibernate比JPA 2有更多的特性。但从实际的角度来看,两者到底有什么区别呢?

我有使用iBatis的经验,现在我正在尝试学习Hibernate或JPA2。我拿起Pro JPA2书,它一直提到“JPA提供商”。例如:

如果你认为一个特性应该标准化,你应该说出来 并向您的JPA提供者请求它

这让我很困惑,所以我有几个问题:

仅使用JPA2,我可以通过简单地注释POJO从DB中获取数据 JPA2应该与“JPA提供者”一起使用吗?例如TopLink或Hibernate?如果是这样,那么与单独使用JPA2或单独使用Hibernate相比,使用JPA2 + Hibernate有什么好处? 你能推荐一本好的实用的JPA2书吗?“Pro JPA2”看起来更像是JPA2的圣经和参考书(直到本书的后半部分才进入查询)。有没有关于JPA2的问题/解决方法的书?


当前回答

JPA是接口,而Hibernate是实现。

传统上有多种Java ORM解决方案:

冬眠 拓联 建分办

每个实现定义自己的映射定义或客户端API。JPA专家组收集了所有这些工具中的精华,因此他们创建了Java Persistence API标准。

从客户端角度来看,标准的持久性API非常方便,可以相对容易地将一个实现转换为另一个实现(尽管实际上并没有那么简单,因为在大型项目中,无论如何都必须使用特定的非标准特性)。

标准JPA将Java ORM的竞争推向了一个新的水平,这只会导致更好的实现。

正如我在《高性能Java持久性》一书中所解释的那样,Hibernate提供了JPA还不支持的特性:

extended identifier generators (hi/lo, pooled, pooled-lo) transparent prepared statement batching customizable CRUD (@SQLInsert, @SQLUpdate, @SQLDelete) statements static or dynamic collection filters (e.g. @FilterDef, @Filter, @Where) and entity filters (e.g. @Where) mapping properties to SQL fragments (e.g. @Formula) immutable entities (e.g. @Immutable) more flush modes (e.g. FlushMode.MANUAL, FlushMode.ALWAYS) querying the second-level cache by the natural key of a given entity entity-level cache concurrency strategies (e.g. Cache(usage = CacheConcurrencyStrategy.READ_WRITE)) versioned bulk updates through HQL exclude fields from optimistic locking check (e.g. @OptimisticLock(excluded = true)) versionless optimistic locking (e.g. OptimisticLockType.ALL, OptimisticLockType.DIRTY) support for skipping (without waiting) pessimistic lock requests support for Java 8 Date and Time support for multitenancy support for soft delete (e.g. @Where, @Filter)

这些额外的特性使Hibernate能够满足大型企业应用程序所需的许多持久性需求。

其他回答

JPA is JSR i.e. Java Specification Requirement to implement Object Relational Mapping which has got no specific code for its implementation. It defines certain set of rules for for accessing, persisting and managing the data between Java objects and the relational databaseWith its introduction, EJB was replaced as It was criticized for being heavyweight by the Java developer community. Hibernate is one of the way JPA can be implemented using te guidelines.Hibernate is a high-performance Object/Relational persistence and query service which is licensed under the open source GNU Lesser General Public License (LGPL) .The benefit of this is that you can swap out Hibernate's implementation of JPA for another implementation of the JPA specification. When you use straight Hibernate you are locking into the implementation because other ORMs may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.

JPA只是一个规范。在市场上有许多实现JPA的供应商。不同类型的供应商以不同的方式实现JPA。所以不同类型的供应商提供不同的功能,所以根据您的需求选择合适的供应商。

如果您正在使用Hibernate或任何其他供应商而不是JPA,那么您不能轻松地将Hibernate迁移到EclipseLink或OpenJPA迁移到Hibernate。但是如果您使用JPA,您只需要在持久性XML文件中更改提供。因此在JPA中很容易实现迁移。

来自维基。

Motivation for creating the Java Persistence API Many enterprise Java developers use lightweight persistent objects provided by open-source frameworks or Data Access Objects instead of entity beans: entity beans and enterprise beans had a reputation of being too heavyweight and complicated, and one could only use them in Java EE application servers. Many of the features of the third-party persistence frameworks were incorporated into the Java Persistence API, and as of 2006 projects like Hibernate (version 3.2) and Open-Source Version TopLink Essentials have become implementations of the Java Persistence API.

正如JCP页面所述,Eclipse链接是JPA的参考实现。再看看这个答案。

JPA本身具有弥补标准ORM框架的功能。由于JPA是Java EE规范的一部分,您可以在项目中单独使用JPA,并且它应该与任何Java EE兼容的服务器一起工作。是的,这些服务器将拥有JPA规范的实现。

Hibernate是最流行的ORM框架,一旦JPA引入,Hibernate就符合JPA规范。除了它应该遵循的基本规范集之外,hibernate还提供了大量额外的东西。

形象地说,JPA只是接口,Hibernate/TopLink类(即接口实现)。

必须有接口实现才能使用接口。但是你可以通过接口使用类,即通过JPA API使用Hibernate,或者你可以直接使用实现,即直接使用Hibernate,而不是通过纯JPA API。

关于JPA的好书是Vlad Mihalcea的《高性能Java持久性》。

JPA只是一个需要具体实现的规范。 现在oracle提供的默认实现是“Eclipselink”。(Toplink由Oracle捐赠给Eclipse基金会,与eclipselink合并)

(参考:http://www.oracle.com/technetwork/middleware/toplink/index-085257.html http://www.eclipse.org/org/press-release/20080317_Eclipselink.php )

使用Eclipselink,可以确保代码在需要时可以移植到任何实现。 Hibernate也是一个完整的JPA实现+ MORE (JPA Plus的一种)。Hibernate是JPA的超级集,具有一些额外的Hibernate特定功能。 因此,在Hibernate中开发的应用程序在切换到其他实现时可能不兼容。 hibernate仍然是大多数开发人员作为JPA实现的选择,并被广泛使用。

另一个JPA实现是OpenJPA (openjpa.apache.org),它是Kodo实现的扩展。