Java 如何使用 JPA 设置 Eclipselink?

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

How to setup Eclipselink with JPA?

javamaven-2jpajakarta-eeeclipselink

提问by deamon

The Eclipselink documentationsays that I need the following entries in my pom.xml to get it with Maven:

EclipseLink的文件说,我需要在我的pom.xml与Maven得到它下面的条目:

<dependencies>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.0.0</version>
    <scope>compile</scope>
       ...
  </dependency>
<dependencies>
      ...
<repositories>
  <repository>
     <id>EclipseLink Repo</id>
     <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
  </repository>    
      ...
</repositories> 

But when I try to use @Entityannotation NetBeans tells me, that the class cannot be found. And indeed: there is no Entity class in the javax.persistence package from Eclipselink.

但是当我尝试使用@EntityNetBeans 告诉我的注释时,找不到该类。事实上:Eclipselink 的 javax.persistence 包中没有实体类。

How do I have to setup Eclipselink with Maven?

我必须如何使用 Maven 设置 Eclipselink?

采纳答案by Pascal Thivent

The eclipselinkartifact doesn't provide the JPA 2.0 API, you need to add javax.persistence:

eclipselink工件不提供 JPA 2.0 API,您需要添加javax.persistence

<repositories>
  <repository>
    <id>eclipselink</id>
    <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo/</url>
  </repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.0.0</version>
    <scope>provided</scope><!-- since I'm running inside a Java EE container -->
  </dependency>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
    <scope>provided</scope><!-- since I'm running inside a Java EE container -->
  </dependency>
  ...

I recommend to use the non OSGI EclipseLink jar for the sake of simplicity.

为简单起见,我建议使用非 OSGI EclipseLink jar。

回答by Mykola Golubyev

You can try to add

您可以尝试添加

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

回答by J?rn Horstmann

When I look into my local maven repository, org.eclipse.persistence:eclipselink does indeed contain the persistence api, at least for version 2.0.0-SNAPSHOT of eclipselink.

当我查看我的本地 maven 存储库时,org.eclipse.persistence:eclipselink 确实包含持久性 api,至少对于 eclipselink 的 2.0.0-SNAPSHOT 版本。

But there is another set of dependencies in the eclipselink repository that are a bit more modularized. These are the dependencies I am using in a current project:

但是 eclipselink 存储库中还有一组更加模块化的依赖项。这些是我在当前项目中使用的依赖项:

<!-- persistence api -->
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
    <scope>provided</scope>
</dependency>
<!-- jpa implementation -->
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa</artifactId>
    <version>2.0.2</version>
    <scope>provided</scope>
</dependency>

Note that scope is set to provided since I deploy to glassfish which already contains eclipselink.

请注意,范围设置为提供,因为我部署到已经包含 eclipselink 的 glassfish。

回答by user454322

Just add the following to your pom.xml.

Now these artifats are in the maven repositories, so no need to add any <repository>

只需将以下内容添加到您的pom.xml.

现在这些工件都在 maven 存储库中,因此无需添加任何<repository>

 <!-- JPA  -->
 <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
</dependency>



Or if you are using a Java EE application server use org.eclipse.persistence.jpa:org.eclipse.persistence, as it doesn't include dependecies that are already on the server.



或者,如果您使用的是 Java EE 应用程序服务器,请使用org.eclipse.persistence.jpa:org.eclipse.persistence,因为它不包括服务器上已有的依赖项。

 <!-- JPA for Java EE application servers  -->
 <dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>org.eclipse.persistence.jpa</artifactId>
  <version>2.5.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>javax.persistence</artifactId>
  <version>2.0.0</version>
  <scope>provided</scope>
</dependency>