Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

like image 285
Brandon Hewlett Avatar asked May 19 '16 15:05

Brandon Hewlett


3 Answers

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.

like image 59
A0__oN Avatar answered Nov 14 '22 01:11

A0__oN


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.

like image 3
Martijn Jan Jaap de Bruin Avatar answered Nov 14 '22 00:11

Martijn Jan Jaap de Bruin


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'
}
like image 2
Abdul Avatar answered Nov 14 '22 00:11

Abdul