无法将JPA依赖项添加到spring-boot项目中

时间:2018-08-24 11:05:44

标签: java spring spring-boot spring-data-jpa

我正在尝试将JPA Maven依赖项添加到已创建的spring-boot项目中,但是出现以下错误:

Error: Could not find or load main class com.kame.demo.DemoApplication

当我删除它时,错误消失了。

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>8.5.32</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

application.properties

spring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.jsp

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:navin

我试图在线找到答案,但没有一个解决方案。

还尝试创建> Spring starter项目>并立即添加JPA,Web和H2,但仍然存在相同的错误。

我正在使用STS IDE,可能与此有关吗?

2 个答案:

答案 0 :(得分:1)

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>

本身就是一个演示项目,具有扩展的SpringBoot类作为您项目的main类。但是这种依赖关系是一样的:

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

因此,两者极有可能发生冲突... 解决方案是为jpa导入正确的依赖关系,而不是spring boot starter jpa依赖关系。

编辑

这也许可以解决问题:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
  </dependency>

但是我建议您阅读正式文档以开始正确使用:https://docs.spring.io/spring-data/jpa/docs/2.1.0.RC2/reference/html/

答案 1 :(得分:0)

在仅使用 Web 创建 Spring Boot 后,我​​尝试在 POM.xml 中手动添加 JPA 依赖项时遇到了同样的问题。

<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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

问题的根本原因:- 缺少 JPA 依赖项 jar。核实:- File->Project structure->Modules->Dependencies,你不会找到任何jar org.springframework.boot:spring-boot-starter-data-jpa:2.4.3(我用的是spring boot 2.4.3 但在你的情况下版本可能不同)

解决方案:- 第 1 步 File -> Invalidate Caches / Restart... 并选择 Invalidate and Restart 选项解决了这个问题。可能是一个陈旧的缓存问题

第 2 步 在 IntelliJ Idea 下打开 POM.xml 右键单击​​-> Maven-> 重新加载项目。 Maven 将下载 JPA 依赖 jar 并将添加到项目中。验证是否添加了 JPA jar。

第 3 步 选择 File -> Invalidate Caches / Restart... 并选择 Invalidate and Restart 选项修复了问题。

注意:- 如果您没有使用 ItellIj id 那么请强制 Maven 下载依赖项(用于 step2)