SpringBoot 2.0.2启动报错:At least one JPA metamodel must be present 求助
问题分析与解决方案
首先,这个错误At least one JPA metamodel must be present!本质是Spring Data JPA找不到任何JPA实体的元模型——哪怕你已经定义了实体类,也可能是以下几个原因导致的,我们逐个排查:
一、实体扫描范围问题
虽然你提到已经有实体类,但Spring可能没扫描到它们:
- 检查你的主启动类(比如
ClashRoyaleSpringBootApplication)的位置:Spring Boot默认会扫描主类所在包及其子包下的实体。如果你的实体类在主类包之外,需要手动添加@EntityScan注解指定实体包路径,示例代码如下:@SpringBootApplication @EntityScan(basePackages = "com.bkates.entity") // 替换成你的实体实际包路径 public class ClashRoyaleSpringBootApplication { public static void main(String[] args) { SpringApplication.run(ClashRoyaleSpringBootApplication.class, args); } }
二、JPA元模型生成配置缺失
Spring Boot 2.0.2.RELEASE对应的Spring Data JPA版本是2.0.7.RELEASE,这个版本在自动生成JPA元模型的逻辑上有变动,需要显式配置来确保元模型被正常生成:
1. Maven插件配置(Maven项目)
在pom.xml中添加Hibernate JPA元模型生成的Maven插件,确保编译时自动生成元模型类:
<build> <plugins> <plugin> <groupId>org.bsc.maven</groupId> <artifactId>maven-processor-plugin</artifactId> <version>3.3.3</version> <executions> <execution> <id>process</id> <goals> <goal>process</goal> </goals> <phase>generate-sources</phase> <configuration> <processors> <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> </processors> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.2.17.Final</version> <!-- 与Spring Boot 2.0.2兼容的版本 --> </dependency> </dependencies> </plugin> </plugins> </build>
2. 配置文件临时调整
你也可以在application.properties中添加配置,临时绕过元模型检查(仅用于验证问题,不推荐长期使用):
spring.data.jpa.repositories.bootstrap-mode=default
三、版本兼容性问题(是否是2.0.2的bug?)
没错,你遇到的情况大概率是Spring Boot 2.0.2.RELEASE的兼容性bug。Spring Boot 2.0.2在整合Spring Data JPA 2.0.7时,确实存在元模型扫描逻辑的问题,不少用户都反馈过类似场景。降级到2.0.1.RELEASE后恢复正常,正是因为2.0.1对应的Spring Data JPA版本是2.0.6.RELEASE,这个版本没有该扫描逻辑的缺陷。
如果不想降级,除了上面的配置修复方案,也可以尝试升级到Spring Boot 2.0.3.RELEASE及以上版本,官方在后续小版本中已经修复了这个问题。
内容的提问来源于stack exchange,提问作者Brian Kates
相关产品推荐
相关产品推荐

