Maven dependencies regarding javax.persistence JAR?

I am using Spring 3 and Hibernate 4 JPA. I am confused regarding javax.persistence JAR. I found below two Maven dependencies on Google. Please tell me which one is required in below two dependencies?

       <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

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

Answers


The first of those javax.persistence.persistence-api is the API Jar, which defines vendor-neutral interfaces which your application should be working with.

The second is the EclipseLink implementation of that API.

It is possible to remove references to the first javax.persistence dependency and just use the EclipseLink jar. However there is a good reason not to.

Writing and compiling your code against the vendor-neutral javax.persistence API ensures that your application code is portable to different persistence providers. For instance if you wished to switch to Hibernate, then you could put that in your pom.xml and remove the org.eclipse dependency, without changing any of your application code.

However, there's an extra little detail you should change. To ensure that your application can switch between persistence providers, the 'implementation' dependency should only be used at runtime. Otherwise, vendor-specific code could easily make its way into your codebase. Add the following to your org.eclipse dependency and see whether your application compiles.

<scope>runtime</scope>

As a result of that, you may find that your application has EclipseLink-specific code in it. This means that you could not change persistence provider without changing your codebase.

Whether that's a problem is up to you. ;)


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

Which is the lastest one and compact with hibernate 4. Also latest version of hibernate support jpa 2.1.0 please check this link


You dont need to include that dependency explicitly, it is in Hibernate pom and will be added transitively


Need Your Help

Mqtt Paho - Trying to publish while broker is unreachable

java groovy mqtt

I'm begininng to use Mqtt and I have a hard time with handling an unreliable network.