java 什么是 JPA 提供程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27420513/
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-11-02 11:47:31  来源:igfitidea点击:

What is a JPA Provider?

javajpa

提问by EnoshBansode

I am new to JPA. As per my understanding JPA itself cannot do persistence. It will need JPA Provider for persisting data into database.

我是 JPA 的新手。根据我的理解,JPA 本身无法做到持久化。它将需要 JPA Provider 将数据持久化到数据库中。

JPA Provider : It is the vendor product that contains the JPA flavor (javax.persistence). For example Eclipselink, Toplink, Hibernate, etc. http://www.tutorialspoint.com/jpa/jpa_orm_components.htm

JPA Provider :它是包含 JPA 风格 (javax.persistence) 的供应商产品。例如 Eclipselink、Toplink、Hibernate 等 http://www.tutorialspoint.com/jpa/jpa_orm_components.htm

So any application which wants to use JPA for persistence it will have to use a Provider like Eclipselink, Toplink, Hibernate, etc. also. Is this correct?

因此,任何想要使用 JPA 进行持久化的应用程序也必须使用像 Eclipselink、Toplink、Hibernate 等的提供程序。这个对吗?

回答by bdulac

To provide further explanations, JPA is an API specified in the frame of the JCP as an answer to a request (e.g JSR 338for JPA 2.1).

为了提供进一步的解释,JPA 是 JCP 框架中指定的 API,作为对请求的回答(例如JSR 338for JPA 2.1)。

Several implementations of that specification exist, the main are:

存在该规范的几种实现,主要是:

In the frame of the Java platform, when a standard API is implemented, this is specified via a system called SPI(for Service Provider Interfaces). Each "vendor" of an implementation has to provide a specific component which is a single interface as a starting point for the implementing classes. The Java tutorial includes an example for the sound API. An implementing class has to be mentioned in a file available to the ClassLoader after the name META-INF/services/{{MyFullInterfaceName}}.

在 Java 平台的框架中,当实现标准 API 时,这是通过称为SPI(用于服务提供者接口)的系统指定的。实现的每个“供应商”都必须提供一个特定的组件,该组件是作为实现类的起点的单个接口。Java 教程包括声音 API示例。在名称META-INF/services/{{MyFullInterfaceName}}之后,必须在 ClassLoader 可用的文件中提及实现类。

For the JPA API, this starting point is the PersistenceProviderinterface (note the spi section in the package name). Each implementation includes the declaration of the implementing class, for exemple in the eclipselink.jaryou can find a file META-INF/services/javax.persistence.spi.PersistenceProvider(named after the full interface name) which contains only the full name of the provider implementation class, in the case of EclipseLink:

对于 JPA API,这个起点是PersistenceProvider接口(注意包名中的 spi 部分)。每个实现都包含实现类的声明,例如在eclipselink.jar 中,您可以找到一个文件META-INF/services/javax.persistence.spi.PersistenceProvider(以完整的接口名称命名),其中仅包含提供者实现类,在 EclipseLink 的情况下:

org.eclipse.persistence.jpa.PersistenceProvider

Most of time the application client of the API does not have to care about that declaration because it is included in the implementation JAR. The only case in which an application has to use that kind of file is when multiple implementations have to be used, for example with EclipseLink and Hibernate:

大多数情况下,API 的应用程序客户端不必关心该声明,因为它包含在实现 JAR 中。应用程序必须使用这种文件的唯一情况是必须使用多个实现,例如使用 EclipseLink 和 Hibernate:

org.eclipse.persistence.jpa.PersistenceProvider
org.hibernate.ejb.HibernatePersistence

You find the implementing class also specified in the persistence.xml file (<provider/> tag).

您会发现在persistence.xml 文件(<provider/> 标记)中也指定了实现类。

Sometime, the JPA provider expression is used to refer to the "vendor" (EclipseLink, Hibernate, etc.) and not to the software component. Both can be considered as valid, depending on the context.

有时,JPA 提供程序表达式用于指代“供应商”(EclipseLink、Hibernate 等)而不是软件组件。根据上下文,两者都可以被认为是有效的。

回答by Master Slave

You got it right, JPAis a specification of the persistence standard. And multiple vendors have implemented the specification, like the ones you mentioned, though note that EclipseLink is based on TopLink, and the further development effort will go primarily to EclipseLink.

您没看错,JPA是持久性标准的规范。并且多个供应商已经实施了该规范,就像您提到的那些,但请注意 EclipseLink 基于 TopLink,进一步的开发工作将主要交给 EclipseLink。

While you can't use JPA without the provider, you can use the vendor implementation directly, but than you lock your self to a specific provider, and lose some benefits, e.g. portability, but also gain some, e.g. features that span beyond specification.

虽然您不能在没有提供程序的情况下使用 JPA,但您可以直接使用供应商实现,但是您将自己锁定到特定的提供程序,并失去一些好处,例如可移植性,但也会获得一些好处,例如跨越规范的功能。

In fact your question, though phrased differently is already answered here What's the difference between JPA and Hibernate?with the following blog emerged from the thread http://blog-tothought.rhcloud.com//post/2

事实上,您的问题虽然措辞不同,但已在此处回答 JPA 和 Hibernate 之间有什么区别?以下博客来自线程 http://blog-tothought.rhcloud.com//post/2