简体   繁体   English

关于javax.persistence JAR的Maven依赖关系?

[英]Maven dependencies regarding javax.persistence JAR?

I am using Spring 3 and Hibernate 4 JPA. 我使用的是Spring 3和Hibernate 4 JPA。 I am confused regarding javax.persistence JAR. 我对javax.persistence JAR很困惑。 I found below two Maven dependencies on Google. 我在Google下面找到了两个Maven依赖项。 Please tell me which one is required in below two dependencies? 请告诉我以下两个依赖项需要哪一个?

       <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

       <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>

The first of those javax.persistence.persistence-api is the API Jar, which defines vendor-neutral interfaces which your application should be working with. 第一个javax.persistence.persistence-api是API Jar,它定义了应用程序应该使用的供应商中立接口。

The second is the EclipseLink implementation of that API. 第二个是该API的EclipseLink实现。

It is possible to remove references to the first javax.persistence dependency and just use the EclipseLink jar. 可以删除对第一个javax.persistence依赖项的引用,只使用EclipseLink jar。 However there is a good reason not to. 但是有充分的理由不这样做。

Writing and compiling your code against the vendor-neutral javax.persistence API ensures that your application code is portable to different persistence providers. 使用供应商中立的javax.persistence API编写和编译代码可确保您的应用程序代码可移植到不同的持久性提供程序。 For instance if you wished to switch to Hibernate, then you could put that in your pom.xml and remove the org.eclipse dependency, without changing any of your application code. 例如,如果您希望切换到Hibernate,那么您可以将其放在pom.xml中并删除org.eclipse依赖项,而无需更改任何应用程序代码。

However, there's an extra little detail you should change. 但是,您应该更改一些额外的细节。 To ensure that your application can switch between persistence providers, the 'implementation' dependency should only be used at runtime. 为了确保您的应用程序可以在持久性提供程序之间切换,“实现”依赖项只应在运行时使用。 Otherwise, vendor-specific code could easily make its way into your codebase. 否则,特定于供应商的代码可以很容易地进入您的代码库。 Add the following to your org.eclipse dependency and see whether your application compiles. 将以下内容添加到org.eclipse依赖项中,并查看应用程序是否编译。

<scope>runtime</scope>

As a result of that, you may find that your application has EclipseLink-specific code in it. 因此,您可能会发现您的应用程序中包含特定于EclipseLink的代码。 This means that you could not change persistence provider without changing your codebase. 这意味着您无法在不更改代码库的情况下更改持久性提供程序。

Whether that's a problem is up to you. 这是否是一个问题取决于你。 ;) ;)

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
</dependency>

Which is the lastest one and compact with hibernate 4. Also latest version of hibernate support jpa 2.1.0 please check this link 哪个是最新的和紧凑的hibernate 4.另外最新版本的hibernate支持jpa 2.1.0请检查此链接

您不需要明确地包含该依赖项,它在Hibernate pom中并且将被传递地添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM