开发者

How to setup Eclipselink with JPA?

The Eclipselink documentation says that I need the following entries in my pom.xml to get it with 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 @Entity annotation NetBeans tells me, that the class cannot be found. And indeed: there is no Entity class in the javax.开发者_开发知识库persistence package from Eclipselink.

How do I have to setup Eclipselink with Maven?


The eclipselink artifact doesn't provide the JPA 2.0 API, you need to add 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.


Just add the following to your pom.xml.

Now these artifats are in the maven repositories, so no need to add any <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.

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


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.

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:

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


You can try to add

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜