我知道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 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只是一个规范,而Hibernate是JPA提供者之一,即Hibernate实现了JPA合同中提到的各种事情。

正如您所说,JPA只是一个规范,这意味着没有实现。您可以尽可能多地使用JPA注释注释您的类,但是如果没有实现,什么也不会发生。将JPA视为必须遵循的指导方针或接口,而Hibernate的JPA实现是满足JPA规范定义的API并提供底层功能的代码。

当您将Hibernate与JPA一起使用时,您实际上是在使用Hibernate JPA实现。这样做的好处是,您可以将Hibernate的JPA实现替换为JPA规范的另一个实现。当你直接使用Hibernate时,你锁定在实现中,因为其他ORM可能使用不同的方法/配置和注释,因此你不能直接切换到另一个ORM。

要了解更详细的描述,请阅读我的博客。

来自维基。

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 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.

我尽量用简单的话解释。

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

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

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

还有一件事

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

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

谢谢你!