Why maven does not support jpa 2.0? - jpa

As far as I know JPA 2.0 has been approved as final in December 2009.
But the central repository gives the pom file declaration for 1.0.2 and 3.0-public_review.
So how can I use the current version of JPA 2.x using maven?

JPA itself is just the API definition, you'll need a provider who has implemented the API. I tend to use hibernate but there are others such as EclipseLink or OpenJPA. If you stick to just the core JPA functionality then you'll be able to swap providers at a later date easily enough.
Your choice of provider may be influenced if you're using a particular EE application server. JPA is part of the EE spec now but each server uses a different provider. E.G. JBoss uses hibernate while glassfish uses EclipseLink I believe.
With that in mind this is how I declare my hibernate dependency in my pom.xml.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.8.Final</version>
</dependency>
This will automatically pull in the JPA 2.0 API jar along with other dependencies such as validation.

Related

How to configure maven to use latest or predefined Spring version / hibernate Version

I am new to Maven and I use STS Eclipse to build spring-maven application. My problem is when I create new spring project by default it adds spring 3.1 and servlet 2.4 version to pom file but I want to add spring 4.2.6 and servlet 4 version. I can edit pom manually but is there any way to configure the maven setting so that it uses the version I provide them.
According to the Spring Boot reference guide :
Each release of Spring Boot provides a curated list of dependencies it
supports. In practice, you do not need to provide a version for any of
these dependencies in your build configuration as Spring Boot is
managing that for you. When you upgrade Spring Boot itself, these
dependencies will be upgraded as well in a consistent way.
The only versions you need to set is the Spring Boot version itself :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
and, if you use some, the versions of libraries unknown from Spring Boot.
If you want to check which version of a dependency is installed by Spring Boot look at the list in the reference guide.

Upgrading GlassFish 3.1.2.2 to use JPA 2.1

I am working with GlassFish 3.1.2.2 (I can not upgrade to 4 due to OS restrictions).
I'm interested in upgrading JPA 2.0 to JPA 2.1 GlassFish 3.1.2.2. How can I achieve this?
This is most likely not possible at all. JPA 2.1 is part of EE 7 and therefore not fully integrated with EE 6 GF 3.1.2.2.
Did you try just replacing the EclipseLink and JPA jar files in Glassfish?
It will probably work, but if you use managed persistence units they will not expose any JPA 2.1 API, you would need to unwrap the EntiyManager to access these.
I'm using Hibernate 4.3.8 (requires JPA 2.1) with Glassfish 3.1.2.2.
Note: I'm not using any services provided by glassfish. All the libraries I use are in the WEB-INF/lib.
1 - Override all JPA classes (package javax.persistence) in glassfish/modules/javax.persistence.jar with JPA 2.1 version. You should not replace the entire JAR, only override the classes. This JAR has an OSGI manifest and other classes that must remain there.
2 - Remove all javassist classes (package javasssist) from glassfish/modules/weld-osgi-bundle.jar. This solves a possible incompatibility if you are using Hibernate.

The import javax.persistence cannot be resolved

I'm currently working on a project that requires EntityManager EntityManagerFacotry and Persistence each from the javax.persistence package. It seems to be for the database service, but the current code is not very well documented. By searching google it seems that there should be an xml file that comes along with this, but there isn't one of those either. I guess my question is simply how do I make these unresolved imports go away? Do I have to add another jar to the build path? It seems that I shouldn't have to since it's been around since 1.5.
Any help is greatly appreciated.
I ran into this same issue and realized that, since I am using spring boot, all I needed to do to resolve the issue was to add the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Yes, you will likely need to add another jar or dependency
javax.persistence.* is part of the Java Persistence API (JPA). It is only an API, you can think of it as similar to an interface. There are many implementations of JPA and this answer gives a very good elaboration of each, as well as which to use.
If your javax.persistence.* import cannot be resolved, you will need to provide the jar that implements JPA. You can do that either by manually downloading it (and adding it to your project) or by adding a declaration to a dependency management tool (for eg, Ivy/Maven/Gradle). See here for the EclipseLink implementation (the reference implementation) on Maven repo.
After doing that, your imports should be resolved.
Also see here for what is JPA about. The xml you are referring to could be persistence.xml, which is explained on page 3 of the link.
That being said, you might just be pointing to the wrong target runtime
If i recall correctly, you don't need to provide a JPA implementation if you are deploying it into a JavaEE app server like JBoss. See here "Note that you typically don't need it when you deploy your application in a Java EE 6 application server (like JBoss AS 6 for example).". Try changing your project's target runtime.
If your local project was setup to point to Tomcat while your remote repo assumes a JavaEE server, this could be the case. See here for the difference between Tomcat and JBoss.
Edit: I changed my project to point to GlassFish instead of Tomcat and javax.persistence.* resolved fine without any explicit JPA dependency.
If anyone is using Maven, you'll need to add the dependency in the POM.XML file. The latest version as of this post is below:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
hibernate-distribution-3.6.10.Final\lib\jpa : Add this jar to solve the issue. It is present in lib folder inside that you have a folder called jpa ---> inside that you have hibernate-jpa-2.0-1.0.1.Final jar
When I ran into this problem, I tracked down enough to fix my problem and move on.
The short version is:
At some point in time Oracle open-sourced J2EE code and the Eclipse foundation took it over.
The transition took a while so information came out during the transition which was transitory in nature. As a result, you might find articles that were only useful during the transition.
The javax.persistence package was moved to a newly named dependency (jakarta.persistence. The persistence package is part of the larger JPA (Java Persistence API). See Intro to JPA.
The Java Persistence API was first released as a subset of the Enterprise JavaBeans 3.0 specification (JSR 220) in Java EE 5. It has since evolved as its own spec, starting with the release of JPA 2.0 in Java EE 6 (JSR 317). JPA was adopted as an independent project of Jakarta EE in 2019. The current release as of this writing is JPA 3.1.
There were issues with SpringBoot pulling in multiple javax.persistence dependencies, Spring-Boot Issue 21220.
Spring and SpringBoot updated their dependencies to use the new location. From Infoq.com, Nov 24, 2022
VMware released the long-anticipated Spring Framework 6 and Spring Boot 3. After five years of Spring Framework 5, these releases start a new generation for the Spring ecosystem. Spring Framework 6 requires Java 17 and Jakarta EE 9 and is compatible with the recently released Jakarta EE 10
If you are on this page looking for answers, most likely it's because your code doesn't compile because it can't find javax.persistence. If this is the case, then you'll either need to:
add the dependency to jakarta.persistence.
Or use older versions of Java and JPA dependencies define classes in the javax.persistence package.
In the future or if you choose to you can rename references from javax.persistence to jakarta.persistence. The same class files in javax.persistence also exist in the jakarta.persistence package.
To fix my problem I added the following dependency:
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
I was using SpringBoot 2.2.2.RELEASE af the time which picked up version 2.2.3 of the jar file (jakarta.persistence-api-2.2.3.jar).
This jar file contained (at least) the following packages:
javax.persistence
javax.persistence.criteria
javax.persistence.metamodel
javax.persistence.spi
based on what my IDE is telling me.
The following articles were helpful for me to get to the solution I needed:
The SO question about this and the answer - https://stackoverflow.com/a/60024749/3281336 which pointed to links I've also included below. Thanks to #Krisz for that.
Explanation of why javax.persistence package was moved to jakarta dependency - https://blogs.oracle.com/javamagazine/post/transition-from-java-ee-to-jakarta-ee This article is good because it gives old dependencies along with the newer dependencies that are needed
SpringBoot 3 & Spring Framework 6 use Jakarta EE 9 - https://www.infoq.com/news/2022/11/spring-6-spring-boot-3-launch/
My solution was to select the maven profiles I had defined in my pom.xml in which I had declared the hibernate dependencies.
CTRL + ALT + P in eclipse.
In my project I was experiencing this problem and many others because in my pom I have different profiles for supporting Glassfish 3, Glassfish 4 and also WildFly so I have differet versions of Hibernate per container as well as different Java compilation targets and so on. Selecting the active maven profiles resolved my issue.
I solved the problem by adding the following dependency
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>2.2</version>
</dependency>
Together with
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
If you are using Gradle with spring boot and spring JPA then add the below dependency in the build.gradle file
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.1.3.RELEASE'
}
In newer hibernate jars, you can find the required jpa file under "hibernate-search-5.8.0.Final\dist\lib\provided\hibernate-jpa-2.1-api-1.0.0.Final". You have to add this jar file into your project java build path. This will most probably solve the issue.
Add this to your dependency if your using maven
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
If you are using Hibernate as a JPA implementation and you are not using Maven/Gradle, the easier way is to download whole bundle instead of jar file one by one.
Go http://hibernate.org/orm/downloads/ and download the latest library, extract the jar from the required folder.
Sad and ashamed to say that after spending 1 hour on same problem (unable to resolve #Entity and javax.persistence) occurring on STS/Eclipse and with all the imports (implementation 'org.springframework.boot:spring-boot-starter-data-jpa'). Turns out it was issue with STS/Eclipse IDE because exactly same code worked on IntelliJ IDE. If nothing works give another IDE a go.
If you are not using Maven/Gradle to import the dependency, simply just download this jar from maven repository and set in build path on Eclipse or your preferred IDE.
https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api/2.2

Using javax.ws.rs jar instead of Jersey or Apache CXF

In java Rest implementation, do we have to use Jersey or Apache CXF or any other implementation ? I mean in dependency wise , I've only used javax.ws.rs and it works fine.
Prasad, the javax.ws.rs includes just API. It won't work on it's own - you need some implementation behind it. If you are saying it is working for you currently, it is probably because you are deploying it to some server that already has an implementation of that API built in (such as GlassFish, WebLogic or any other JavaEE 6 compliant server - since JAX-RS is part of EE6, all EE6 compliant app. servers would have some implementation of that API - GF and WLS use Jersey, JBoss uses RESTEasy, some other servers may use something else).
Jersey is the open source JAX-RS (JSR 311) Reference Implementation for building RESTful Web services.
I've used Jersey a lot, and, expected in rare configuration cases, you won't notice it — just use JAX-RS. Apache CXF was builded for SOAP and adapted to ReST, it's far more complicated than Jersey, IMO.
The Jersey Maven dependencies are the following:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.14</version>
</dependency>

How to add HSQLDB library to SpringMVC in Maven?

I would like to ask how to add HSQLDB library hsqldb.jar in Maven? I have project in Eclipse created as Spring Template Project from SpringSource Tool Suite. It uses Maven as far as I know. I would like my project to use and work with HSQL database. And at this moment I want my Tomcat server to use HSQLDB library file. So If I added this library file in Maven, my project would work properly with HQL database. This is my assumption.
I have found this example http://slu.livejournal.com/5965.html but adding the jar file to the class path isn't very good programmig practice, is it?
I'm newbie in Maven and also in Spring Framework.
Newest version:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.8</version>
<scope>runtime</scope>
</dependency>
Remember about runtime scope in such artifacts (most people forget about it), since you use some generic API/SPI (ORM or JDBC probably) for database access and HSQLDB is only some kind of vendor for run-time implementation of such API/SPI.