Search code examples
kotlinmicronaut-data

How to use @Transient with Micronaut Data and Kotlin properties that lack a backing field?


I'm moving to Micronaut Data JDBC and Kotlin from Spring Data JDBC and Java, and have trouble with @Transient on a Kotlin property which does not have a backing field.

Example code:

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> {
}

At runtime I get a complaint from Postgres:

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

Hmmm, looks like Micronaut Data wants to serialize/deserialize the inherited property. So I tried @Transient on the property, and compilation fails with:

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

Suggestions on how to solve this?


Solution

  • interface Foo {
        @get:javax.persistence.Transient
        val doodah: Boolean
            get() = /* some default implementation */
    }