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>
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. ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With