Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 213
deamon Avatar asked Jun 09 '10 06:06

deamon


3 Answers

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.

like image 85
Pascal Thivent Avatar answered Oct 05 '22 22:10

Pascal Thivent


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>
like image 21
user454322 Avatar answered Oct 06 '22 00:10

user454322


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.

like image 44
Jörn Horstmann Avatar answered Oct 06 '22 00:10

Jörn Horstmann