64

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.

Matt
  • 5,408
  • 14
  • 52
  • 79
  • 5
    You'll indeed need a jar file with the JPA API. But more importantly, you'll need an implementation of these JPA interfaces (Hibernate, EclipseLink, OPenJPA, etc.). Frankly, trying to use JPA without even knowing what it is, and which implementation has been chosen, is a sure path to disaster. If you inherit code, it should come with a build process at the very least. – JB Nizet Mar 24 '13 at 12:10
  • Interesting @JB Nizet, the problem is occurring for me in my Google App Engine integration and following the step-by-step instructions what are way out of date. The integration module is supposed to be bundled so the Android App does not have to deal with the details. There is no mention of JPA and there is mention that MAVEN will download and include all dependent SDK files, etc. – mobibob Dec 31 '13 at 20:21

14 Answers14

60

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>
Dave McLure
  • 839
  • 1
  • 8
  • 10
  • Thanks, that worked for me – pixel Oct 06 '21 at 21:02
  • 1
    new to Java... which file does this go in? – Markus Jul 22 '22 at 14:59
  • Totally saved me! just added to the pom file and worked like a charm – bluewolfxD Oct 08 '22 at 01:07
  • 3
    Looks like this no longer works in SpringBoot 3 (`org.springframework.boot:spring-boot-starter-parent:3.0.0`). Not sure what the SpringBoot 3 equivalent is (yet). – PatS Dec 12 '22 at 19:26
  • @PatS me new to Spring Boot, you found the solution, no? – nart Jan 03 '23 at 02:03
  • 3
    @nart, yes I did. I posted an answer which might help you and others. Several other answers have information that also helps but I tried to summarize a timeline that explains how/why this happened. See https://stackoverflow.com/a/74996933/3281336 for my answer – PatS Jan 03 '23 at 17:59
44

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>
Mindsect Team
  • 2,311
  • 5
  • 29
  • 36
38

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.

Community
  • 1
  • 1
typoerrpr
  • 1,626
  • 21
  • 18
30

The other answers didn't work for me and none described the root of the problem.

Once I understood the root of the problem, I understood why different people found different answers to this question.

The short version is:

  1. At some point in time Oracle open-sourced J2EE code and the Eclipse foundation took it over.

  2. 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.

  3. 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.

  1. There were issues with SpringBoot pulling in multiple javax.persistence dependencies, Spring-Boot Issue 21220.

  2. 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

  1. 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:

PatS
  • 8,833
  • 12
  • 57
  • 100
  • 1
    It is lengthy but it is worth reading the solution, came across this problem multiple times until I read this @PatS. Ideally this should be the accepted answer – EnthuCoder May 21 '23 at 14:27
29

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

Omar
  • 1,430
  • 1
  • 14
  • 31
Karthik Reddy
  • 2,902
  • 1
  • 18
  • 9
  • Didn't come naturally to me so adding steps (for intelliJ) 1. Go to project structure (in file menu) 2. Add library button 3. Start typing hibernate and then search in the bar with full name and matching version, add that into project and then you are good – Niraj Motiani Mar 21 '23 at 05:50
2

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.

500 Server error
  • 644
  • 13
  • 28
2

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'
} 
iamkdblue
  • 3,448
  • 2
  • 25
  • 43
Tarun Jain
  • 262
  • 1
  • 8
2

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>
Roberto
  • 4,524
  • 1
  • 38
  • 30
1

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.

Umesh_Shah
  • 49
  • 4
1

Add this to your dependency if your using maven

     <dependency> 
       <groupId>javax.persistence</groupId>   
       <artifactId>javax.persistence-api</artifactId>
       <version>2.2</version>
     </dependency>
  • 2
    As of Nov 2020, this dependency was moved to jakarta.persistence. See https://mvnrepository.com/artifact/jakarta.persistence/jakarta.persistence-api/3.0.0. Note: Hibernate 5.5 uses it. – PatS Jan 03 '23 at 19:37
0

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.

Sam YC
  • 10,725
  • 19
  • 102
  • 158
0

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.

nayakasu
  • 889
  • 1
  • 11
  • 32
0

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

Subhajit Roy
  • 111
  • 3
  • 14
0

for those whose problem didnt get solved by the first comment , for spring boot 3 its import jakarta.persistence.*;

steven stonie
  • 26
  • 1
  • 3