【问题标题】:Spring Data JDBC - Kotlin support - Required property not found for classSpring Data JDBC - Kotlin 支持 - 找不到类所需的属性
【发布时间】:2021-01-01 10:32:53
【问题描述】:

我正在尝试将 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())
}

但这对我没有帮助,我仍然遇到这个错误。

我该如何解决这个问题?

【问题讨论】:

  • 我个人建议使用 Java 而不是 Kotlin 的 JPA 数据类。
  • 这不是JPA,这是JDBC,请多加注意。 Spring Data JDBC 很好地支持 Kotlin,见 official docs

标签: spring kotlin spring-data-jdbc


【解决方案1】:

事实证明,我的第二次尝试就是解决方案。

诀窍在于我的运行/调试配置以进行测试。

在 IDEA Preferences 中,我选中了 Preferences | Build, Execution, Deployment | Build Tools | Maven | Runner — Delegate IDE build/run actions to 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())
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 2019-10-10
    • 2019-01-26
    • 2021-01-23
    • 2022-08-10
    • 1970-01-01
    • 2013-11-04
    • 2017-05-17
    相关资源
    最近更新 更多