简体   繁体   中英

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>

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 .

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' }

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 Project but 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.

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM