Spring Boot:是否存在@Entity注释? [英] Spring Boot: does @Entity Annotation exist?
问题描述
我看了这个Spring Boot教程: https: //javabrains.io/courses/spring_bootquickstart/lessons/Creating-a-Spring-Data-JPA-Repository
I watched this Spring Boot tutorial: https://javabrains.io/courses/spring_bootquickstart/lessons/Creating-a-Spring-Data-JPA-Repository
,它将@Entity注释放在类名之前.当我尝试在代码中执行此操作时,出现一条错误消息,提示无法解析@Entity".
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".
这是我的pom.xml文件:
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?
推荐答案
我最近也遇到了这个问题.对我来说,spring-boot-starter-data-jpa
依赖关系是个问题.尽管它在pom.xml
文件中没有任何错误,但是jars不在类路径中-即使在同步maven并以不同方式多次导入依赖项之后(mvn clean/update/install).
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>
我使用的IDE是IntelliJ Idea.
The IDE I was using was IntelliJ Idea.
最后对我有用的东西:我只是将此依赖关系放在<dependencies>
... </dependencies>
标记内的所有其他依赖关系上,即只是更改了依赖关系的顺序并进行了同步.就我而言,这行得通,不知道为什么.
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.
下面是我的pom.xml
文件内容供参考.
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>
这篇关于Spring Boot:是否存在@Entity注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!