我尝试将 Spring Data JDBC 与 Kotlin 数据类结合使用,在将 @Transient
属性添加到主构造函数后,我在简单的 findById
调用中收到错误:
java.lang.IllegalStateException: Required property transient not found for class mitasov.test_spring_data_with_kotlin.Entity!
我的实体类如下所示:
data class Entity(
@Id
var id: String,
var entityName: String,
@Transient
var transient: List<TransientEntity>? = mutableListOf(),
)
阅读后that issue我尝试在没有 @Transient
字段的情况下创建 @PersistenseConstructor
:
data class Entity(
@Id
var id: String,
var entityName: String,
@Transient
var transient: List<TransientEntity>? = mutableListOf(),
) {
@PersistenceConstructor
constructor(
id: String,
entityName: String,
) : this(id, entityName, mutableListOf())
}
但这对我没有帮助,我仍然收到该错误。
如何解决这个问题?
最佳答案
事实证明,我的第二次尝试就是解决方案。
诀窍在于我的测试运行/调试配置。
在 IDEA 首选项中,我检查了 Preferences |构建、执行、部署 |构建工具| Maven | Runner — 将 IDE 构建/运行操作委托(delegate)给 Maven
复选框,这意味着我需要在运行测试之前手动重新编译我的项目。
解决方案
所以,这就是错误的解决方案
java.lang.IllegalStateException: Required property transient not found for class mitasov.test_spring_data_with_kotlin.Entity!
正在制作没有@Transient
字段的@PersistenseConstructor
:
data class Entity(
@Id
var id: String,
var entityName: String,
@Transient
var transient: List<TransientEntity>? = mutableListOf(),
) {
@PersistenceConstructor
constructor(
id: String,
entityName: String,
) : this(id, entityName, mutableListOf())
}
关于Spring Data JDBC - Kotlin 支持 - 未找到类所需的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65528934/