无法在Springboot Gradle项目中解析@Entity和javax.persistence.Entity

时间:2018-10-10 07:27:25

标签: java mysql spring spring-boot gradle

我正在按照https://spring.io/guides/gs/accessing-data-mysql/上的指南开始将MySQL用于Gradle项目,但是,当我尝试在@Entity注释中添加所有内容时

enter image description here

这是我的build.gradle,它在网站上遵循相同的内容:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-accessing-data-mysql'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")

    // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile group: 'javax.persistence', name: 'persistence-api', version: '1.0'

    // Use MySQL Connector-J
    compile 'mysql:mysql-connector-java'

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

2 个答案:

答案 0 :(得分:1)

删除compile group: 'javax.persistence', name: 'persistence-api', version: '1.0',然后再尝试以下操作,因为jar可能已损坏。

  1. 运行./gradlew clean install,然后检查。.

  2. 如果上述方法不起作用:然后delete ~/.gradle/caches并再次运行gradle install命令以重新导入所有依赖项。

  3. 重新启动IDE,然后再次导入项目。

答案 1 :(得分:0)

正如您在SpringBoot的依赖树中看到的那样,javax.persistence已经包含在spring-boot-starter-data-jpa中,而不是1.0

enter image description here

删除:

compile group: 'javax.persistence', name: 'persistence-api', version: '1.0'