I'm building a Java Spring Boot web app, using Maven and Java 15. Since I've been refactoring my DAOs to use Spring Boot Starter Data Jpa, i've just noticed that my Entity classes are unable to find jakarta.persistence package for use of annotations such as @Entity (I'm unsure if this refactor is related, but seems suspicious--as far as I know I didn't change any dependencies or delete any libraries, except adding "spring-boot-starter-data-jpa" to my pom.xml). Previously, jakarta.persistence was imported normally and my app ran as expected, persisting these entities without issue.
I can see the external library in my project: "Maven: jakarta.persistence:jakarta.persistence-api:2.2.3" which holds the jakarta.persistence jar.
I've even added
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.3</version>
</dependency>
to my pom.xml to try and formally add jakarta.persistence to my project, and reloaded it, but no luck.
When I use IntelliJ to auto import now, it auto imports javax.persistence for @Entity annotation.
Here is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myGroupId</groupId>
<artifactId>myArtifact</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/jakarta.persistence/jakarta.persistence-api -->
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.4.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.centralbanker.CentralBankerApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<!-- non-parallel tests for dao testing, but didn't work-->
<!-- <configuration>-->
<!-- <forkCount>1</forkCount>-->
<!-- <reuseForks>true</reuseForks>-->
<!-- </configuration>-->
</plugin>
</plugins>
</build>
</project>
Spring Boot 2 uses the javax.*
package name space.
Spring Boot 3 uses jakarta.*
.
See Preparing for Spring Boot 3.0.
And see Wikipedia.
Java 15 is past end-of-life. Use either the latest, Java 20, or one of the Long-Term Support (LTS) versions: 8, 11, 17 (and likely 21). Spring Boot 3 requires Java 17+.
Do not mix generations of JUnit.
Use the aggregator archetype of JUnit 5 for everything you need to run modern Jupiter tests. Add JUnit Vintage Engine to also run legacy tests from JUnit 3 & 4.