3

This is my first time really playing around with Java development using Eclipse. I am trying to use EclipseLink's implementation of the JPA. I moved all of my entities into a separate package "entities". I have the persistence.xml in a separate JPA project called "dataModeling".

package explorer

Everything builds and runs fine.

Just about every project depends on my entities. However, I'm seeing a warning Class javax.persistence.Entity not found - continuing with a stub., etc. showing up because the dependent projects don't reference EclipseLink.

warning messages

The solution is to go into each dependent project's properties and under Java Build Path > Libraries, click Add Library, then User Library and then select EclipseLink.

However, to me, it doesn't make sense to reference EclipseLink in every project! That's an implementation detail I don't want to burden other projects with. It looks like this is happening because the other projects see the annotations and don't recognize them.

So the real question is: how can I use JPA (via annotations) without every other project needing to reference my JPA implementation?

Travis Parks
  • 8,435
  • 12
  • 52
  • 85
  • The **javax.persistence.Entity** is a class of the library **javaee.jar**. Do you have this jar in your project classpath? – Mark Korzhov Jul 17 '14 at 20:27
  • @MarkKorzhov Bear with me, here. That class is already defined in the EclipseLink library. What is the point of referencing it from the `javaee.jar`? I'm trying to avoid adding references to bunch of projects when they don't know anything about JPA. – Travis Parks Jul 17 '14 at 20:38
  • persistence-api.jar (aka javax.persistence.jar). You do NOT need javaee.jar (unless you want to pull in the rest of JavaEE!!!) – Neil Stockton Jul 18 '14 at 03:10
  • @NeilStockton What's strange is there are a half dozen implementations of javax.persistence, including J2EE7, Hibernate, EclipseLink, etc. When I search `persistence-api.jar`, the first thing I find is a maven link (which I'm not using). – Travis Parks Jul 18 '14 at 12:21
  • 1
    Javax.persistence classes are just interfaces for the JPA specification, of which Hibernate and EclipseLink are providers, and J2EE containers are required to implement as well. So you'll find a persistence.jar in a whole bunch of places, as they all need it to work. Since your entities have references to javax.persistence classes, any project that needs your entities will also need the javax.persistence classes. Those other projects will only need the Eclipselink jar if they are going to use an EntityManager/JPA in some way – Chris Jul 18 '14 at 13:08
  • 1
    @Travis Parks. Yes I know, the "JPA people" were incredibly lazy in not being bothered to provide a standard persistence-api.jar (already documented elsewhere), when it should be a standard provision of any JCP "project". Tell that to Oracle. – Neil Stockton Jul 18 '14 at 14:05
  • In my eclipse plugin directory, there was a `javax.persistence._.jar` file. Adding it to my **Libraries** section made the warnings go away. One of the reasons I didn't want to directly reference EclipseLink everywhere was that it was cross-compiled with an older version of Scala, which caused another warning. Just referencing the `javax.persistence` jar removes all warnings. This jar probably showed up when I added tje Dali and EclipseLink plugins. – Travis Parks Jul 19 '14 at 01:20

2 Answers2

10

Your pom.xml should contain:

<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>
    <scope>compile</scope>
</dependency>

the first one is Eclipse-Link (which you already have), the second one is Persistence API which is lacking.

If you are not using maven - make sure that javax.persistence-2.0.0.jar is on your classpath.

Note that this is version 2.0.0, the newest is 2.1.0

update The project which makes use of EntityManager should have these dependences. Putting entities and persistence.xml in separate jar file still requires the other project that uses it to fulfill above dependencies.

Maciej Dobrowolski
  • 11,561
  • 5
  • 45
  • 67
  • I'm not using Maven (not really sure what it is, actually). So it sounds like because my entities are decorated with annotations, any eclipse project referencing them also must reference EclipsleLink. Is there a way to avoid using these annotations? – Travis Parks Jul 17 '14 at 22:34
  • @TravisParks look at the update - the project where you use `EntityManager` should have these dependencies. – Maciej Dobrowolski Jul 18 '14 at 08:34
  • Currently it does have those dependencies. I still get warnings for every project referencing my entities. – Travis Parks Jul 18 '14 at 12:16
3

Thanks to @neil-stockton and @chris, I was able to figure out what was going on. Most JPA implementations have a copy of the javax.persistence JAR floating around somewhere. Most of them are bundled with everything else, leading to my dependency nightmare. There doesn't appear to be a de facto implementation floating around.

In my case, I used the copy that showed up under my Eclipse plugins directory. These annotations were truly empty in that did not have any unwanted dependencies. The JAR file (javax.persistence._<version>.jar) only showed up after I added the Dali and EclipseLink plugins (one or the other).

Travis Parks
  • 8,435
  • 12
  • 52
  • 85