Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"package javax.persistence does not exist" using spring-data-jpa and IntelliJ

I'm starting a project using spring-data-jpa in IntelliJ but I am failing to load javax.persistence ("package javax.persistence does not exist").

I have been at it for four hours with what seems be be a very simple problem. I used the standard IntelliJ UI to create the project and selected the spring framework and spring-data-jpa options.

My only code is:

import javax.persistence.entity;

@entity
public class customer {

}

The project fails to build and the tool tips in the IDE say "Can not resolve symbol persistence" What am I missing?

My directory structure can be found below: enter image description here

like image 783
pmaurais Avatar asked Nov 16 '25 03:11

pmaurais


2 Answers

All I needed to do was add the hibernate libs from maven through the project structure dialog (hibernate-entitymanager)

like image 152
pmaurais Avatar answered Nov 18 '25 20:11

pmaurais


In java 11 (released 2018), the java EE libraries were moved from the built-in core to optional dependencies (See http://openjdk.java.net/jeps/320). So you may need to add:

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

(hibernate-entitymanager has this as a dependency so that's probably why adding it fixed the problem - see https://repo1.maven.org/maven2/org/hibernate/hibernate-entitymanager/5.6.15.Final/hibernate-entitymanager-5.6.15.Final.pom)

Also related: In 2017, Java EE was transferred to Eclipse and renamed to Jakarta EE (Oracle owned the name "Java").

The latest imports are now under "jakarta": eg import jakarta.persistence.EntityManager;.

Hibernate 5 still uses JPA 2.2 (javax) rather but Hibernate 6 uses JPA 3.0 (jakarta). Ensure you do not have both hibernate 5 and 6 in your mvn dependency:tree.

like image 33
Curtis Yallop Avatar answered Nov 18 '25 20:11

Curtis Yallop