I am following this tutorial https://spring.io/guides/tutorials/rest
At the first step when I added class Employee by copying from the tutorial, it says package javax.persistence does not exist
I added dependencies given in the tutorial. I generated the initial project using spring initializr as described in the tutorial. My pom is below:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.trupt</groupId>
<artifactId>cure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cure</name>
<description>cure</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SDK selected in the IDE is here
Even in this simple step this is not working.
I have tried maven reimport, maven clean-install, invalidate caches and restart. None of these worked.
Should I add a dependency? Isn’t spring data starter enough?
This is really hard.
>Solution :
This guide is written in a version of spring-boot < 3.x it mean that all javax. imports was moved to jakarta. And you are using <version>3.1.4</version>
It mean that every imports also from guide will be
FROM
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
TO:
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
Also I suggest you to have a look at preparing-for-spring-boot-3-0