【问题标题】:How to use @Transient with Micronaut Data and Kotlin properties that lack a backing field?如何将 @Transient 与缺少支持字段的 Micronaut Data 和 Kotlin 属性一起使用?
【发布时间】:2019-11-23 17:33:37
【问题描述】:

我正在从 Spring Data JDBC 和 Java 迁移到 Micronaut Data JDBC 和 Kotlin,并且在没有支持字段的 Kotlin 属性上遇到 @Transient 问题。

示例代码:

interface Foo {
    // @JvmDefault here had no effect on my problem
    // @Transient does not compile here
    val doodah: Boolean
        get() = /* some default implementation */
}

// Bar implements Foo for reasons unrelated to this question, part of an internal "microframework"
@Entity
@Introspected
@Table(name = "bar")
data class Bar(@Id var id: Long /* , more properties */) : Foo {
}

@JdbcRepository(dialect = POSTGRES)
interface BarRepository : CrudRepository<Bar, Long> {
}

运行时我收到来自 Postgres 的投诉:

org.postgresql.util.PSQLException: ERROR: column child_record_.doodah does not exist

嗯,看起来 Micronaut Data 想要序列化/反序列化继承的属性。所以我在属性上尝试了@Transient,编译失败:

This annotation is not applicable to target 'member property without backing field or delegate'

关于如何解决这个问题的建议?

【问题讨论】:

    标签: kotlin micronaut-data


    【解决方案1】:
    interface Foo {
        @get:javax.persistence.Transient
        val doodah: Boolean
            get() = /* some default implementation */
    }
    

    【讨论】:

    • 这也会产生编译错误:```此注解不适用于目标'无支持字段或委托的成员属性'并使用站点目标'@get'```
    • @binkley 你确定你使用的是正确的注解类(javax.persistence.Transient)吗?
    • 你是完全正确的。我引入了 Kotlin JVM 注释,而不是持久性注释。你的答案是正确的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-12-21
    • 2018-07-13
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2020-02-04
    • 1970-01-01
    相关资源
    最近更新 更多