且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-12 20:28:24

第一个 .persistence.persistence-api 是API Jar,它定义了应用程序应该使用的与供应商无关的接口。



第二个是该API的EclipseLink实现。



可以删除对第一个 javax.persistence 依赖项的引用,使用EclipseLink jar。然而,有一个很好的理由不要这样做。



编写和编译代码与供应商无关 javax.persistence API确保您的应用程序代码可移植到不同的持久性提供程序。例如,如果你希望切换到Hibernate,那么你可以把它放到你的pom.xml中,并且删除 org.eclipse 依赖项,而不用改变你的任何应用程序代码。 / p>

但是,您应该更改一些额外的细节。为了确保您的应用程序可以在持久性提供程序之间切换,只应在运行时使用实现依赖性。否则,供应商特定的代码很容易进入您的代码库。将以下内容添加到 org.eclipse 依赖项中,并查看您的应用程序是否已编译。

 <范围>运行时间< /范围> 

因此,您可能会发现您的应用程序中包含特定于EclipseLink的代码。这意味着你不能在不改变代码库的情况下更改持久性提供者。



这是否是个问题取决于您。 ;)

I am using Spring 3 and Hibernate 4 JPA. I am confused regarding javax.persistence JAR. I found below two Maven dependencies on Google. 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.

The second is the EclipseLink implementation of that API.

It is possible to remove references to the first javax.persistence dependency and just use the 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. 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.

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.

<scope>runtime</scope>

As a result of that, you may find that your application has EclipseLink-specific code in it. This means that you could not change persistence provider without changing your codebase.

Whether that's a problem is up to you. ;)