Java 什么是 JPA 实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4477082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:54:20  来源:igfitidea点击:

What is a JPA implementation?

javaeclipsejpaeclipselink

提问by user262525

I'm getting started with JPA, and I'm confused as to what exactly the JPA implementation(EclipseLink, TopLink, Hibernate, etc.) does.

我正在开始使用 JPA,但我对 JPA 实现(EclipseLink、TopLink、Hibernate 等)究竟做了什么感到困惑。

I understand the theoretical role of JPA, but what is the purpose of the various implementations? Are there significant differences between these choices, as there is with say, DB connectors/drivers? Which is the best one for a beginner?

我了解 JPA 的理论作用,但各种实现的目的是什么?这些选择之间是否存在显着差异,正如所说的 DB 连接器/驱动程序?哪个是最适合初学者的?

I'll probably go with EclipseLink because that is what most of the literature I've read uses.

我可能会使用 EclipseLink,因为这是我读过的大部分文献都使用的。

采纳答案by darioo

JPA

日本特许经营协会

JPA is just an API (hence Java Persistence API) that requires an implementation to use.

JPA 只是一个需要实现才能使用的 API(因此是 Java Persistence API)。

An analogy would be using JDBC. JDBC is an API for accessing databases, but you need an implementation (a driver jar file) to be able to connect to a database. On its own, without a driver, you cannot do anything with a database.

一个类比是使用 JDBC。JDBC 是用于访问数据库的 API,但您需要一个实现(驱动程序 jar 文件)才能连接到数据库。就其本身而言,没有驱动程序,您无法对数据库进行任何操作。

With JPA, as I said, you need an implementation, a set of classes that lie "below" JPA, and such an implementation will do what you want.

使用 JPA,正如我所说,您需要一个实现,一组位于 JPA“下方”的类,这样的实现将执行您想要的操作。

Your application uses the JPA API (this wording is a bit cubersome, but I hope you get the idea), which then communicates with the underlying implementation.

您的应用程序使用 JPA API(这个措辞有点复杂,但我希望您能理解),然后与底层实现进行通信。

Popular implementations include Hibernate, EclipseLink, OpenJPAand others.

流行的实现包括HibernateEclipseLinkOpenJPA等。

Every one of them implement the JPA API, so if you use only JPA, every implementation should act the same.

它们中的每一个都实现了 JPA API,因此如果您只使用 JPA,那么每个实现的行为都应该相同。

But! The functionality provided by these implementations might go beyond the standard JPA API.

但!这些实现提供的功能可能超出标准 JPA API。

If you want to use this particular functionality, you will have to use vendor specific API that will not be compatible with others.

如果要使用此特定功能,则必须使用与其他供应商不兼容的供应商特定 API。

For example, even though JPA defines the @Idannotation with ID generation options, when using Hibernate, you can use also @org.hibernate.annotations.GenericGeneratorfor Hibernate specific generation strategies.

例如,即使 JPA 定义了@Id带有 ID 生成选项的注解,当使用 Hibernate 时,您也可以使用@org.hibernate.annotations.GenericGeneratorHibernate 特定的生成策略。

Using this annotation will not work unless you're using Hibernate as the underlying implementation.

除非您使用 Hibernate 作为底层实现,否则无法使用此注释。

The bottom line is: JPA is the "least common denominator" which every vendor implements, and every implementation might have some more advanced features that aren't standard.

底线是:JPA 是每个供应商实现的“最小公分母”,并且每个实现都可能具有一些非标准的更高级功能。

If you want your application to be portable, use only JPA. If you are sure you won't change your mind later on and switch implementations, use JPA + vendor specific features.

如果您希望应用程序可移植,请仅使用 JPA。如果您确定以后不会改变主意并切换实现,请使用 JPA + 供应商特定功能。

回答by Eran Medan

It's the same as Java, it has a specification (Java SE, Java EE) and implementation, most people use the reference implementation (By Sun / Oracle) which is a blueprint for others to make their own "better" and "advanced" implementation of the specification (APIs, Interfaces and documentation / JavaDoc if you need a one word definition, although it is much more than that)

它和 Java 一样,有规范(Java SE、Java EE)和实现,大多数人使用参考实现(由 Sun / Oracle),这是其他人做出自己“更好”和“高级”实现的蓝图规范(API、接口和文档/JavaDoc,如果您需要一个词的定义,尽管它远不止于此)

So the Java / JDK you are most likely using is just an implementation (that is very popular). There are many more implementations out there, some have more Garbage Collection configuration options, some claim to be faster, and some are just not worth using.

所以你最有可能使用的 Java/JDK 只是一个实现(非常流行)。还有更多的实现,有些有更多的垃圾收集配置选项,有些声称更快,有些只是不值得使用。

So the implementation basically does everything, it has the code, where the JPA is an API (implied by it's name, Java Persistance API) e.g. again, more about interfaces and documentation than real implentation of the interface.

所以实现基本上做了所有事情,它有代码,其中 JPA 是一个 API(由它的名称暗示,Java Persistance API)例如,更多的是关于接口和文档而不是接口的真正实现。

Think of it as the abstact class that Hibernate's JPA implementation / EclipseLink extends / implements

把它想象成 Hibernate 的 JPA 实现/EclipseLink 扩展/实现的抽象类

Regarding JPA, as opposed to Hibernate which has a single implementation, the idea is to decouple the interface from the implementation, so that the "best" implementation will win and that everyone can participate in the game, not just the interface creator.

关于 JPA,与 Hibernate 具有单一实现相反,其想法是将接口与实现解耦,以便“最佳”实现获胜,并且每个人都可以参与游戏,而不仅仅是接口创建者。

This allows for instance for Google App Engine to support JPA, as it just needs to follow an API (DataNucleus is the implementation) if it has to use Hibernate, it would have required to modify the Hiberanate release to support Google's Big Table format (the fact that JPA in GAE is not that great is another story)

例如,这允许 Google App Engine 支持 JPA,因为它只需要遵循 API(DataNucleus 是实现),如果它必须使用 Hibernate,则需要修改 Hiberanate 版本以支持 Google 的 Big Table 格式(事实上,GAE 中的 JPA 并不是那么好是另一回事)

回答by supercena

JPA itself is just a specification, not a product; it cannot perform persistence or anything else by itself. JPA is just a set of interfaces, and requires an implementation. There are open-source and commercial JPA implementations to choose from and any Java EE 5 application server should provide support for its use. JPA also requires a database to persist to.

JPA 本身只是一个规范,而不是一个产品;它本身无法执行持久性或其他任何操作。JPA 只是一组接口,需要一个实现。有可供选择的开源和商业 JPA 实现,任何 Java EE 5 应用服务器都应为其使用提供支持。JPA 还需要一个数据库来持久化。

回答by Badal

JPA is a Java API specification which describes the management of relational data in applications using Java Platform. You can think JPA as a set of Rules, and hence it can not do any concrete work for you but helps you to standardize your implementation.

JPA 是一种 Java API 规范,它描述了使用 Java 平台的应用程序中关系数据的管理。您可以将 JPA 视为一组规则,因此它不能为您做任何具体的工作,而是帮助您标准化您的实现。

So you need concrete implementation which will implements JPA (set of rules). Hibernate, OpenJPA are few of famous JPA implementations.

所以你需要具体的实现来实现 JPA(规则集)。Hibernate、OpenJPA 是少数著名的 JPA 实现。

回答by Faizan Mohammad

The Java Persistence API (JPA) is a Java programming language application programming interface specification that describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition. The Java Persistence API originated as part of the work of the JSR 220 Expert Group of the Java Community Process. JPA 2.0 was the work of the JSR 317 Expert Group.

Java Persistence API (JPA) 是一种 Java 编程语言应用程序编程接口规范,它描述了使用 Java Platform, Standard Edition 和 Java Platform, Enterprise Edition 的应用程序中关系数据的管理。Java Persistence API 起源于 Java Community Process 的 JSR 220 专家组工作的一部分。JPA 2.0 是 JSR 317 专家组的工作。

So, JPA provides set of interfaces which must be implemented. There are always reference implementations available for language specifications , in this case EclipseLink is the reference implementation.

因此,JPA 提供了一组必须实现的接口。总是有可用于语言规范的参考实现,在这种情况下 EclipseLink 是参考实现。

Hibernate on the other hand, does implement JPA specification but it also has it's own native API.

另一方面,Hibernate 确实实现了 JPA 规范,但它也有自己的原生 API。

For Example :

例如 :

Say you designed an Authentication specification which contains only one interface :

假设您设计了一个仅包含一个接口的身份验证规范:

public interface AccessAuthenticator {
    public boolean verifyAccess(String username,String saltedPassword);
}

Now this specification contains only one interface and one single method.

现在这个规范只包含一个接口和一个方法。

In order to use this specification(more precisely the verifyAccess( ) method) you must have an implementation.

为了使用这个规范(更准确地说是 verifyAccess() 方法),你必须有一个实现。

There can be many implementations for this spec. for example, some organization may provide it's implementation for authentication to it's own servers. you must have that implementation in order to connect to the organizations server (There may be some native API involved).

这个规范可以有很多实现。例如,某些组织可能会为其自己的服务器提供身份验证的实现。您必须具有该实现才能连接到组织服务器(可能涉及一些本机 API)。

One more aspect of providing a specification is that you don't actually use the concrete implementation of anything , so today say you are using hibernate to connect to your database but tomorrow some better ORM comes in picture that implements JPA spec, then all you need to do is change the dependency in your project (in simple words replace the implementation JAR file/Edit the pom file).

提供规范的另一个方面是您实际上并没有使用任何具体实现,所以今天说您使用 hibernate 连接到您的数据库,但明天会出现一些更好的 ORM 来实现 JPA 规范,那么您只需要要做的是更改项目中的依赖项(简单来说,替换实现 JAR 文件/编辑 pom 文件)。