Brandon Hewlett
Brandon Hewlett

Reputation: 247

Spring Boot - javax Import statements not working correctly

I just started working with Spring Boot, and am currently following this tutorial with some small modifications to naming and using their own version of Eclipse, which has this generator built in.

When I get to the first snippet of code, I try just copying over the import statements to start with,

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.HashSet;
import java.util.Set;

which gets me the following error with the javax imports:

The import javax.persistence.[insert name here] cannot be resolved

When I find the javax.persistence package, I find that, sure enough, the starter code provided from their own service does not contain the listed packages. I'm left confused and wondering if I did something wrong in the initial steps. Anyone have any ideas?

Edit 1: pom content provided

<?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.Me</groupId>
<artifactId>petstore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>petstore</name>
<description>Petstore Project</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>

Upvotes: 15

Views: 84955

Answers (9)

Achyuth23
Achyuth23

Reputation: 411

If you are using Spring Boot 3, you have to import jakarta dependencies into your pom.xml.

javax used to work with the import of spring boot jpa earlier up until Spring Boot 2.X. javax is now migrated to jakarta since Spring Boot 3.X, now you have to import the following dependency:

<dependency>
     <groupId>jakarta.persistence</groupId>
     <artifactId>jakarta.persistence-api</artifactId>
</dependency>

Now, replace your import javax.persistance.*; with import jakarta.persistence.*;

That's it. Select your project, right click > Maven > Update Project, will take care of the import errors.

Upvotes: 1

Khader M A
Khader M A

Reputation: 6351

You can first import

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

and use

import jakarta.persistence.Entity;

as javax.persistence.* is renamed to jakarta.persistence.*

Upvotes: 0

Perhaps this will help someone: apparently Javax persistence api is renamed to Jakarta persistence. So you will have to import jakarta.persistence instead of javax.persistence.

Upvotes: 61

H&#252;sna POYRAZ
H&#252;sna POYRAZ

Reputation: 45

Solution steps:

  1. open terminal
  2. Go to root directory with "cd" command
  3. list invisible folders with "ls -a"
  4. you should see the .m2 file. There are all the maven repository on the .m2 file
  5. Delete maven with command “sudo rm -r .m2
  6. Run the project again

Upvotes: 0

Abdul
Abdul

Reputation: 1208

Please refer the docs. https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes

As of #19550, Web and WebFlux starters do not depend on the validation starter by default anymore. If your application is using validation features, for example you find that javax.validation.* imports are not being resolved, you’ll need to add the starter yourself.

For Maven builds, you can do that with the following:

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

For Gradle, you will need to add something like this:

dependencies {
  ...
  implementation 'org.springframework.boot:spring-boot-starter-validation'
}

Upvotes: 4

Orio Ryo
Orio Ryo

Reputation: 188

I had the same issue, but previous solutions didn't work for me. I do solve the issue by adding following dependency to POM file.

<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>

Upvotes: 2

Harun ERGUL
Harun ERGUL

Reputation: 5940

In my case i have added below spring boot data dependency but still not be able to import javax.persistence.*

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

I realized that eclipse somehow not be able to adding this dependencies to my classpath. Firstly i tried right click on the project Maven>Update Projectbut still not worked. Then I execute mvn eclipse:eclipse inside the root directory and then after successful build i refreshed project in the eclipse. This time it worked.

Upvotes: 1

Cpt Kitkat
Cpt Kitkat

Reputation: 1402

Add this dependency in you pom.xml

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>

After that Right click on your project and in maven option update your project. That should fix it.

Upvotes: 1

A0__oN
A0__oN

Reputation: 9110

Add spring-boot-starter-data-jpa dependency

If you are using Maven add to pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

If you are using Gradle add in build.gradle

compile "org.springframework.boot:spring-boot-starter-data-jpa"

for other refer this.

Upvotes: 36

Related Questions