Spring Boot - javax Import statements not working correctly
Asked Answered
T

9

15

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>

Thermoplastic answered 19/5, 2016 at 15:24 Comment(4)
pom content pleaseWidescreen
@AmanTuladhar Pom content addedThermoplastic
have you downloaded the jar dependencies via maven?Thermoluminescent
Have you added the spring-boot-starter-data-jpa dependencyWidescreen
W
35

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.

Widescreen answered 19/5, 2016 at 15:36 Comment(5)
That seems to have worked, thank you. Odd that it didn't include that dependency at the start.Thermoplastic
What about dependency version?Cheder
@Cheder too late to reply but you need to add it dependency like this : implementation group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '6.0.0.Alpha6', ext: 'pom'Barraza
@Cheder the version is already defined via dependencyManagement in the parent pom defined at the top: spring-boot-starter-parent. This is the "spring boot way": the concrete version that is compatible is defined for you. You just add the dependency artifact without version. That way you don't accidentally override the 'good' version and cause incompatibilities.Bingle
The gradle method compile is now replaced by implementation. So do implementation 'org.springframework.boot:spring-boot-starter-data-jpa' instead.Kussell
S
58

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.

Striated answered 18/7, 2022 at 21:21 Comment(2)
This was it for meNorth
I was searching for javax. Now got it. Thanks.Overscore
W
35

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.

Widescreen answered 19/5, 2016 at 15:36 Comment(5)
That seems to have worked, thank you. Odd that it didn't include that dependency at the start.Thermoplastic
What about dependency version?Cheder
@Cheder too late to reply but you need to add it dependency like this : implementation group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '6.0.0.Alpha6', ext: 'pom'Barraza
@Cheder the version is already defined via dependencyManagement in the parent pom defined at the top: spring-boot-starter-parent. This is the "spring boot way": the concrete version that is compatible is defined for you. You just add the dependency artifact without version. That way you don't accidentally override the 'good' version and cause incompatibilities.Bingle
The gradle method compile is now replaced by implementation. So do implementation 'org.springframework.boot:spring-boot-starter-data-jpa' instead.Kussell
L
4

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'
}
Lindquist answered 6/2, 2021 at 16:46 Comment(0)
D
2

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>
Divisionism answered 23/1, 2021 at 15:13 Comment(0)
S
1

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.

Subsonic answered 24/9, 2018 at 10:26 Comment(1)
Thank you CPT Kitkat. I was missing the reload...Complexity
H
1

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.

Hinshelwood answered 4/5, 2020 at 10:18 Comment(0)
P
1

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.

Perni answered 3/5 at 11:16 Comment(0)
I
0

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
Instar answered 11/8, 2023 at 9:51 Comment(0)
C
0

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.*

Cammi answered 27/1 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.