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

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

Maven dependencies regarding javax.persistence JAR?

javaspringhibernatemavenjpa

提问by user755806

I am using Spring 3 and Hibernate 4 JPA. I am confused regarding javax.persistenceJAR. I found below two Maven dependencies on Google. Please tell me which one is required in below two dependencies?

我正在使用 Spring 3 和 Hibernate 4 JPA。我对javax.persistenceJAR感到困惑。我在 Google 上发现了以下两个 Maven 依赖项。请告诉我以下两个依赖项中需要哪一个?

       <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>

采纳答案by Steve

The first of those javax.persistence.persistence-apiis 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.persistencedependency and just use the EclipseLink jar. However there is a good reason not to.

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

Writing and compiling your code against the vendor-neutral javax.persistenceAPI 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.eclipsedependency, without changing any of your application code.

针对供应商中立javax.persistenceAPI编写和编译您的代码可确保您的应用程序代码可移植到不同的持久性提供程序。例如,如果您希望切换到 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.eclipsedependency 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. This means that you could not change persistence provider without changing your codebase.

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

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

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

回答by Sureshkumar Panneerselvan

<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 请检查此链接

回答by Evgeniy Dorofeev

You dont need to include that dependency explicitly, it is in Hibernate pom and will be added transitively

您不需要显式包含该依赖项,它位于 Hibernate pom 中并且将被传递添加