Spring Kotlin项目创建entityManagerFactory Bean报错:未知访问类型record
问题分析与解决方案
错误Unknown access type record的核心原因是依赖冲突与不合理的依赖配置,结合你的Spring Boot 3.2.2环境,按以下步骤修复:
1. 清理冲突/冗余依赖
你的Gradle配置里存在多个明显问题:
- 重复依赖:
spring-boot-starter-data-jpa已经包含spring-data-jpa、Hibernate核心及Jakarta Persistence API,无需手动添加这些单独依赖 - 过时冲突依赖:
hibernate-entitymanager在Hibernate 5.x之后就整合到hibernate-core里了,6.x版本根本没有这个单独依赖,你加的6.0.0.Alpha7是极旧的预览版,和hibernate-core:6.4.4.Final严重冲突jakarta.persistence-api:3.2.0-M1是不稳定的里程碑版本,Spring Boot 3.2.2自带稳定的API版本,手动指定反而会引发冲突io.spring.gradle:dependency-management-plugin是Gradle插件,应该放在plugins块里,你已经在plugins中声明了io.spring.dependency-management,这个依赖完全多余
- 过老的H2版本:
h2:1.3.148是十多年前的版本,不兼容Java 21和Spring Boot 3.x
2. 修正后的Gradle配置
替换你的dependencies块为如下内容:
dependencies { // Spring核心依赖 implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0") // 数据库依赖(仅运行时需要) runtimeOnly("com.h2database:h2:2.2.224") // Axon依赖 implementation("org.axonframework:axon-spring:4.9.2") // Kotlin相关依赖 implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2") }
注意:plugins块保持你原来的配置即可,不要在dependencies里添加插件依赖。
3. 检查状态聚合类实现
如果修复依赖后仍有问题,检查你的Axon State Aggregate类:
- 不要用Kotlin的
record类型作为需要JPA持久化的聚合类(Hibernate对record的支持需要特定配置,演示场景用普通类+data class更稳妥) - 确保聚合类正确标注
@Aggregate、@AggregateIdentifier等Axon注解,同时如果作为JPA实体,需标注@Entity、@Id等JPA注解(Axon的JPA存储会自动处理,但类结构要符合JPA要求)
4. 补充H2配置建议
在application.properties中添加H2的基础配置,确保数据库正常连接:
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true
内容的提问来源于stack exchange,提问作者chhu
相关产品推荐
相关产品推荐

