Reputation: 14988
I watched this Spring Boot tutorial: https://javabrains.io/courses/spring_bootquickstart/lessons/Creating-a-Spring-Data-JPA-Repository
and it puts an @Entity annotation before a class name. When I tried to do it in my code, I got an Error saying "can't resolve @Entity".
Here is my pom.xml file:
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
<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>org.apache.derby</groupId>
<artifactId>derby</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>
Do you know what is the problem?
Upvotes: 15
Views: 79719
Reputation: 9
Using NetBeans IDE (Version 22), after adding the dependency in the pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Is necesary to do Build for your project to download the jar files and they can appear inside the library Dependencies:
Example of Jar file downloaded
After this, the IDE can find the dependency and the @Entity annotation works.
Upvotes: 0
Reputation: 11
I added the javax.persistence in the pom.xml file as a dependency, but it still required the version, so I grabbed a version from mvn repositories, this is what my dependency looks like, this was using eclipse editor. And it worked.
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
Upvotes: 0
Reputation: 16
I had the same issues recently and i resolved it using adding this dependency that given below. I think the version change in spring boot made this error, now javax.persistence
is not available instead we can use jakarta.persistence
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.6.Final</version>
<type>pom</type>
</dependency>
Upvotes: 0
Reputation: 11
What Worked For Me In Last:-
Upvotes: 1
Reputation: 101
i faced same issue, after add this dependencies fixed my issue
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
Upvotes: 0
Reputation: 1
Upvotes: 0
Reputation: 1146
Most of the answers here are hit and try types which doesn't explain why @Entity annotation was not working and why it started working after adding certain dependencies.
Here's how it works-
For using persistence annotations you need to add relevant dependencies with their version, let's say we add spring-boot-starter-data-jpa as follows-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.6.6</version>
</dependency>
Now when you are using Intellij Idea as your IDE, you need to reload your maven project to re read the dependencies from your pom.xml.
Once you do that, @Entity annotation would be available for use.
Upvotes: 1
Reputation: 21
Click on the red bulb next to the @Entity annotation. Choose "Add dependency" - choose "javax.persistence.api" (NOT jakarta)
Upvotes: 0
Reputation: 266
Seems you are working with EclipseLink JPA implementation,
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
Spring Data JPA works with Hibernate implementation by default. We need to exclude unnecessary Hibernate dependencies from spring-boot-starter-data-jpa
when working with other JPA implementations.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
If you are working with Hibernate, just spring-boot-starter-data-jpa
is enough in your case.
I also seen "can't resolve @Entity" issue because of my IDE. In eclipse, click Project option -> Clean the project. This is solved my issue when project dependencies out of sync with IDE.
Upvotes: 0
Reputation: 11
Do a force update mvn clean install -U
or you can right click on your project -->maven--> update
if you are using eclipse/STS. this synchronizes the IDE with pom dependencies.
Upvotes: 0
Reputation: 1252
I added this dependency
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
Add this and enjoy.
Upvotes: 4
Reputation: 11
I had the same problem. After I configured the new dependency on pom.xml
file, the @Entity
annotation is not found because these dependencies is not found. To solve it, the IntelliJ IDE
then suggested me to load the Maven changes. On the pom.xml
screen, click on the icon to reload Maven.
Upvotes: 1
Reputation: 129
Use spring-boot-starter-data-jpa
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
It works for me ... :)
Do not use the following one
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
Upvotes: 12
Reputation: 11
In my case, by adding the spring-boot-starter-data-jpa dependency to the classpath it worked.
Upvotes: 1
Reputation: 21
Spring STS usually shows this error when the set up isn't correct.
Follow the rest so that the project then re-aligns the import order.
Upvotes: 2
Reputation: 16
put this in your file at the top as an import statement
import javax.persistence.*;
Upvotes: -2
Reputation: 51
Make sure you Maven projects are imported successfully.
In the lower right corner of your IntelliJ there will a prompt asking you if you want to import the changes. It will also ask you if you want to enable Auto-Import.
Upvotes: 0
Reputation: 373
I also faced this problem recently. For me, the spring-boot-starter-data-jpa
dependency was the issue. Although it was in the pom.xml
file without any errors, but the jars were not there in classpath - even after syncing maven and importing dependencies multiple times in different ways ( mvn clean/update/install).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
The IDE I was using was IntelliJ Idea.
What worked for me at last : I just put this dependency over all other dependencies inside the <dependencies>
... </dependencies>
tag, i.e just changed the order of dependencies and synced. That worked in my case, not sure why.
Below is my pom.xml
file contents for reference.
<?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>dev.sanskar.courses</groupId>
<artifactId>course-api</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<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>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Upvotes: 20
Reputation: 616
suppose you have missed a dependency.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
There are useful sample spring boot projects in this github page.it will be helpful to have a look.
Upvotes: 1