JPA和Hibernate有什么区别? [关闭]

本文翻译自:What's the difference between JPA and Hibernate? [closed]

I understand that JPA 2 is a specification and Hibernate is a tool for ORM. 我知道JPA 2是一个规范,而Hibernate是ORM的工具。 Also, I understand that Hibernate has more features than JPA 2. But from a practical point of view, what really is the difference? 另外,我了解到Hibernate具有比JPA 2更多的功能。但是从实际的角度来看,真正的区别是什么?

I have experience using iBatis and now I'm trying to learn either Hibernate or JPA2. 我有使用iBatis的经验,现在我正在尝试学习Hibernate或JPA2。 I picked up Pro JPA2 book and it keeps referring to "JPA provider". 我拿起Pro JPA2书籍,并且一直指“ JPA提供者”。 For example: 例如:

If you think a feature should be standardized, you should speak up and request it from your JPA provider 如果您认为某个功能应该被标准化,则应该大声疾呼并向您的JPA提供者提出要求

This confuses me so I have a few questions: 这使我感到困惑,所以我有几个问题:

  • Using JPA2 alone can I fetch data from DB by simply annotating my POJO's 我可以单独使用JPA2注释POJO即可从数据库中获取数据
  • Is JPA2 supposed to be used with a "JPA Provider" eg TopLink or Hibernate? JPA2是否应该与“ JPA Provider”一起使用,例如TopLink或Hibernate? If so, then what's the benefit of using JPA2 + Hibernate as compared to JPA2 alone, or compared to Hibernate alone ? 如果是这样,那么与单独使用JPA2或单独使用Hibernate相比,使用JPA2 + Hibernate有什么好处?
  • Can you recommend a good practical JPA2 book. 您能推荐一本实用的JPA2书籍吗? "Pro JPA2" seems more like a bible and reference on JPA2 (It doesn't get into Queries until the later half of the book). “ Pro JPA2”似乎更像是一本关于JPA2的圣经和参考书(直到本书的后半部分才进入Queries)。 Is there a book that takes a problem/solution approach to JPA2? 是否有一本书针对JPA2采用问题/解决方法?

#1楼

参考:https://stackoom.com/question/fSep/JPA和Hibernate有什么区别-关闭


#2楼

JPA is just a specification which needs concrete implementation. JPA只是需要具体实现的规范。 The default implementation oracle provide is "Eclipselink" now. oracle提供的默认实现现在是“ Eclipselink”。 (Toplink is donated by Oracle to Eclipse foundation to merge with eclipselink) (Toplink由Oracle捐赠给Eclipse基金会以与eclipselink合并)

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

Using Eclipselink, one can be sure that the code is portable to any implementation if need arises. 使用Eclipselink,可以确保代码在需要时可移植到任何实现中。 Hibernate is also a full JPA implementation + MORE ( Sort of JPA Plus). Hibernate也是完整的JPA实现+ MORE(JPA Plus的排序)。 Hibernate is super set of JPA with some extra Hibernate specific functionality. Hibernate是JPA的超级集,具有一些额外的Hibernate特定功能。 So app developed in Hibernate may not be compatible when switched to other implementation. 因此,当切换到其他实现时,由Hibernate开发的应用可能不兼容。 Still hibernate is choice of majority of developers as JPA implementation and widely used. 休眠状态仍然是大多数开发人员的选择,因为JPA实现并被广泛使用。

Another JPA implementation is OpenJPA (openjpa.apache.org) which is an extension of Kodo implementation. 另一个JPA实现是OpenJPA(openjpa.apache.org),它是Kodo实现的扩展。


#3楼

JPA is a Java API specification which describes the management of relational data in applications using Java Platform. JPA是Java API规范,它描述了使用Java平台的应用程序中关系数据的管理。 where as Hibernate is a ORM (Object Relational Mapping) library which follows JPA specification. 其中Hibernate是遵循JPA规范的ORM(对象关系映射)库。

You can think JPA as a set of Rules which is implemented by Hibernate. 您可以将JPA视为由Hibernate实现的一组规则。


#4楼

JPA是舞者,Hibernate是舞者。


#5楼

Some things are too hard to understand without a historical perspective of the language and understanding of the JCP. 如果没有语言的历史视角和对JCP的理解,有些事情将很难理解。

Often there are third parties that develop packages that perform a function or fill a gap that are not part of the official JDK. 通常,有第三方开发的软件包可以执行某些功能或填补空白,而这并不是官方JDK的一部分。 For various reasons that function may become part of the Java JDK through the JCP (Java Community Process) 由于各种原因,该功能可能会通过JCP(Java社区流程)成为Java JDK的一部分。

Hibernate (in 2003) provided a way to abstract SQL and allow developers to think more in terms of persisting objects (ORM). Hibernate(在2003年)提供了一种抽象SQL的方法,并允许开发人员在持久对象(ORM)方面进行更多思考。 You notify hibernate about your Entity objects and it automatically generates the strategy to persist them. 您通知hibernate有关您的Entity对象的信息,它会自动生成策略以将其持久化。 Hibernate provided an implementation to do this and the API to drive the implementation either through XML config or annotations. Hibernate提供了一个实现此目的的实现,并提供了API通过XML配置或批注来驱动实现。

The fundamental issue now is that your code becomes tightly coupled with a specific vendor(Hibernate) for what a lot of people thought should be more generic. 现在的根本问题是,您的代码与特定的供应商(休眠)紧密结合在一起,很多人认为这应该更通用。 Hence the need for a generic persistence API. 因此,需要通用的持久性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). 同时,JCP在Hibernate和其他ORM工具供应商的大力支持下,正在开发JSR 220(Java规范请求),从而产生了JPA 1.0(2006年),最终产生了JSR 317,即JPA 2.0(2009年)。 These are specifications of a generic Java Persistence API. 这些是通用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. JDK以一组接口的形式提供了API,因此您的类可以依赖于javax.persistence,而不必担心正在进行对象持久化工作的特定供应商。 This is only the API and not the implementation. 这只是API,而不是实现。 Hibernate now becomes one of the many vendors that implement the JPA 2.0 specification. Hibernate现在成为实现JPA 2.0规范的众多供应商之一。 You can code toward JPA and pick whatever compliant ORM vendor suits your needs. 您可以对JPA进行编码,并选择适合您需求的任何兼容ORM供应商。

There are cases where Hibernate may give you features that are not codified in JPA. 在某些情况下,Hibernate可能会为您提供JPA中未编写的功能。 In this case, you can choose to insert a Hibernate specific annotation directly in your class since JPA does not provide the interface to do that thing. 在这种情况下,您可以选择直接在类中插入特定于Hibernate的注释,因为JPA不提供执行该操作的接口。

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


#6楼

JPA is just a specification.In market there are many vendors which implements JPA. JPA只是一个规范。在市场上有许多实施JPA的供应商。 Different types of vendors implement JPA in different way. 不同类型的供应商以不同的方式实现JPA。 so different types of vendors provide different functionality so choose proper vendor based on your requirements. 因此不同类型的供应商提供不同的功能,因此请根据您的要求选择合适的供应商。

If you are using Hibernate or any other vendors instead of JPA than you can not easily move to hibernate to EclipseLink or OpenJPA to Hibernate.But If you using JPA than you just have to change provide in persistence XML file.So migration is easily possible in JPA. 如果您使用的是Hibernate或其他任何供应商而不是JPA,则无法轻松地将其休眠到EclipseLink或将OpenJPA转换为Hibernate。但是如果您使用的是JPA,则只需要更改持久性XML文件中的提供即可。 JPA。

你可能感兴趣的:(java,hibernate,jpa,java-ee,orm)