我知道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的问题/解决方法的书?


当前回答

来自维基。

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还提供了大量额外的东西。

其他回答

如果没有语言的历史视角和对JCP的理解,有些事情就很难理解。

通常会有第三方开发包来执行官方JDK不包含的功能或填补空白。由于各种原因,该函数可能通过JCP (Java社区进程)成为Java JDK的一部分。

Hibernate(2003年)提供了一种抽象SQL的方法,并允许开发人员更多地从持久化对象(ORM)的角度进行思考。你通知hibernate关于你的Entity对象,它会自动生成持久化它们的策略。Hibernate提供了实现和API,通过XML配置或注释来驱动实现。

现在的基本问题是,您的代码与特定的供应商(Hibernate)紧密耦合,而许多人认为这应该是更通用的。因此需要通用的持久性API。

Meanwhile, the JCP with a lot of input from Hibernate and other ORM tool vendors was developing JSR 220 (Java Specification Request) which resulted in JPA 1.0 (2006) and eventually JSR 317 which is JPA 2.0 (2009). These are specifications of a generic Java Persistence API. The API is provided in the JDK as a set of interfaces so that your classes can depend on the javax.persistence and not worry about the particular vendor that is doing the work of persisting your objects. This is only the API and not the implementation. Hibernate now becomes one of the many vendors that implement the JPA 2.0 specification. You can code toward JPA and pick whatever compliant ORM vendor suits your needs.

在某些情况下,Hibernate可能会提供JPA中没有编码的特性。在这种情况下,您可以选择直接在类中插入Hibernate特定的注释,因为JPA没有提供接口来做这件事。

来源:http://www.reddit.com/r/java/comments/16ovek/understanding_when_to_use_jpa_vs_hibernate/

来自维基。

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是一个Java API规范,它描述了使用Java平台管理应用程序中的关系数据。其中Hibernate是一个ORM(对象关系映射)库,遵循JPA规范。

您可以将JPA看作是由Hibernate实现的一组规则。

JPA只是一个规范,而Hibernate是JPA提供者之一,即Hibernate实现了JPA合同中提到的各种事情。

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.