Spring Boot 3+Hibernate出现org/hibernate/type/ByteType类找不到错误求助
问题原因分析
出现java.lang.NoClassDefFoundError: org/hibernate/type/ByteType错误的核心原因是依赖版本不兼容,具体如下:
- JPA API 冲突:Spring Boot 3.x 基于 Jakarta EE 9,依赖的是
jakarta.persistence-api,但你手动引入了旧的javax.persistence-api(2.2版本),这会导致JPA规范API的版本冲突,进而影响Hibernate的类型处理逻辑。 - QueryDSL 版本未适配:你使用的QueryDSL版本未明确指定,且旧版本的QueryDSL(低于5.x)会依赖Hibernate 5.x的API,而Hibernate 6.x(Spring Boot 3.x默认集成)已经移除了
org.hibernate.type.ByteType这个类,导致调用fetchCount()时找不到该类。 - 手动指定Spring Data JPA版本:你手动指定了
spring-data-jpa3.0.0版本,破坏了Spring Boot父项目的依赖版本管理,可能引发潜在的版本不匹配问题。 - 旧版APT插件:使用的
com.mysema.maven:apt-maven-plugin是QueryDSL旧版插件,与Spring Boot 3.x的Jakarta环境不兼容,可能导致注解处理器生成的代码存在兼容性问题。
解决方案
1. 移除冲突依赖
删除pom.xml中以下两个依赖:
<!-- 移除手动指定的Spring Data JPA,由Spring Boot父项目管理版本 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>3.0.0</version> </dependency> <!-- 移除旧版JPA API,Spring Boot starter已包含正确的Jakarta版本 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency>
2. 指定兼容的QueryDSL版本
在pom.xml的<properties>节点中添加QueryDSL版本(需适配Spring Boot 3.x,推荐5.0.0及以上):
<properties> <java.version>17</java.version> <querydsl.version>5.0.0</querydsl.version> </properties>
3. 替换旧版APT插件
删除原有的com.mysema.maven:apt-maven-plugin,替换为QueryDSL官方的新插件:
<plugin> <groupId>com.querydsl</groupId> <artifactId>querydsl-maven-plugin</artifactId> <version>${querydsl.version}</version> <executions> <execution> <goals> <goal>process</goal> </goals> <configuration> <outputDirectory>target/generated-sources/java</outputDirectory> <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor> <classpathScope>compile</classpathScope> </configuration> </execution> </executions> </plugin>
4. 清理并重新构建项目
执行以下Maven命令清理缓存并重新构建:
mvn clean install
完成以上步骤后,fetchCount()方法应该能正常执行,不会再出现找不到ByteType类的错误。
内容的提问来源于stack exchange,提问作者beatfloraminederecho
相关产品推荐
相关产品推荐

