I'm trying to build an implementation agnostic maven module which relies on JPA2. Unfortunately, the only Maven JPA dependency is JPA1 based, and consequently, I cannot use EntityManager.detach() method as that is a JPA2 option only.
Ideally, I'd love to be able to specify my javax.persistence dependency in my Pom, and require the app/container to supply the JPA2 implementation. Unfortunately, I cannot find any such dependency.
Is my only choice at this point to declare hibernate-jpa-2.0-api 1.0.0.FINAL as a provided dependency?
The Hibernate JPA 2 classes (javax.persistence...) are here:
This are the most relevant and official API for JPA2 from EclipseLink:
Unlike Hibernates API this are from official specification and support better generics when it comes to use Criteria API.
I know this is a quite old post, if you want to go agnostic from the implementation, then you should use the Java EE API dependency instead.
Just add to your POM:
Where the ${jee.version} is your desired Java EE version. I'm currently using 7.0. It has all EJB, JPA and JSF APIs dependencies.
I use the
javax.persistence
artifact (and not theeclipselink
artifact) from the EclipseLink Maven repository to access the JPA 2 API classes. Snippets from POM include:The
javax.persistence
artifact contains all the API classes, and none of the EclipseLink classes (except for two), allowing you to specify the scope as provided; this applies even for the EclipseLink JPA provider as well (which is in theeclipselink
artifact Id).I haven't mixed the
javax.persistence
artifact with thehibernate-entitymanager
artifact, which is how I managed the dependency for another project that relies on Hibernate EntityManager instead of EclipseLink for the JPA provider. A snippet from the second project's POM is shown below:I do change the dependency scopes from
provided
totest
in other projects to ensure that unit tests will have a JPA provider in the classpath. This is primarily done to mask out the side-effects of using thejavaee-api
dependency, which I use in the parent POM to allow compile time references to several Java EE 6 API classes.I was able to solve resolve my Maven JPA2 dependency by adding a couple of dependencies to the project's pom.xml file. See below for the xml code for the dependencies.
I found the latest groupId's and artifactId's versions by drilling down the directories out on Maven Central. I just walked the directory tree until I found the metadata.xml files for the persistence.core and the persistence.jpa artifacts.
Note: The dependency on persistence.jpa is what actually brings in the javax.persistence jar.
For the JPA 2.2 API an "official" artifact is available from maven central, see this answer.