我知道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能够满足大型企业应用程序所需的许多持久性需求。

其他回答

我尽量用简单的话解释。

假设你需要一辆车,我们都知道他们有几个a级制造商,如梅赛德斯,宝马,奥迪等。

现在在上面的声明中,CAR(是一种规格),因为每辆车都有共同的特征,比如有四个轮子的东西,可以在道路上行驶。所以它像JPA。 奔驰、宝马、奥迪等只是使用常见的汽车功能,并根据他们的客户基础添加功能,所以他们正在实现汽车规范,如hibernate、iBATIS等。

因此,通过这个通用特性,jpa和hibernate只是一个实现,根据他们的jboss的需要。

还有一件事

JPA包括一些基本属性,所以在未来,如果你想改变hibernate到任何其他实现,你可以很容易地切换,而不需要太多的头痛,这些基本属性包括JPA注释,可以工作于任何实现技术,JPQL查询。

因此,我们主要使用JPA类型技术实现hibernate,只是为了以防万一我们想根据客户需要切换我们的实现,加上您将编写更少的代码,因为JPA中涉及一些常见的特性。 如果有人仍然不清楚,那么你可以评论,因为我是新的堆栈溢出。

谢谢你!

JPA is Java Persistence API. Which Specifies only the specifications for APIs. Means that the set of rules and guidelines for creating the APIs. If says another context, It is set of standards which provides the wrapper for creating those APIs , can be use for accessing entity object from database. JPA is provided by oracle.When we are going to do database access , we definitely needs its implementation. Means JPA specifies only guidelines for implementing APIs. Hibernate is a JPA provider/Vendor who responsible for implementing that APIs. Like Hibernate TopLink and Open JPA is some of examples of JPA API providers. So we uses JPA specified standard APIs through hibernate.

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实现的扩展。

JPA是规范,Hibernate是遵循规范中规定的规则的实现提供者。

Java——它不仅独立于操作系统,而且独立于供应商。

因此,您应该能够在不同的应用程序服务器上部署应用程序。 JPA是在任何Java EE兼容的应用服务器上实现的,它允许交换应用服务器,但是实现也会发生变化。Hibernate应用程序可能更容易部署在不同的应用程序服务器上。