You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级至Spring Boot 3后无法使用hibernate-jpamodelgen求助

Spring Boot 3.2.6升级后hibernate-jpamodelgen编译错误解决

问题分析

升级到Spring Boot 3.2.6后编译出现NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager,核心原因:

  • Spring Boot 3.2.6对应Hibernate ORM 6.x版本,hibernate-jpamodelgen编译时需要hibernate-commons-annotations,但默认间接引入的该依赖是runtime scope,编译阶段无法访问。
  • 手动将hibernate-commons-annotations改为compile后,又会缺失jboss-jandex——hibernate-jpamodelgen依赖它处理注解索引,进而触发新的NoClassDefFoundError。

解决办法

方法1:显式添加编译依赖

在pom.xml中补充编译级别的缺失依赖,确保编译阶段能获取所有必需类:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-envers</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <scope>provided</scope> <!-- 仅编译阶段使用,不打包到最终产物 -->
</dependency>
<!-- 显式添加编译所需依赖 -->
<dependency>
    <groupId>org.hibernate.common</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.jboss.jandex</groupId>
    <artifactId>jandex</artifactId>
    <scope>compile</scope>
</dependency>

方法2:通过Maven编译插件配置注解处理器

这是Maven推荐的注解处理器使用方式,能自动处理依赖传递,避免scope冲突:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.hibernate.orm</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

配置后,你可以保留原hibernate-jpamodelgen依赖(设为optional或provided),也可以直接移除——插件会通过annotationProcessorPaths自动引入编译所需的注解处理器依赖,不会影响运行时环境。

内容的提问来源于stack exchange,提问作者Martin Mucha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 04:45:12