Kotlin、JPA 和@Transient

Kotlin, JPA and @Transient

我有一个实体 class,我已将其从 Java 迁移到 Kotlin。其中一个属性 (inetAddress) 被注释为 @Transient 以防止它被持久化到关系数据库中。但是,JPA(Hibernate 实现)看不到此注释并将其作为自己的字段放在数据库中。当 class 写在 Java.

中时,这一切都正常工作

有谁知道如何正确注释此 Kotlin class 以便 JPA/Hibernate 将看到 @Transient 注释而不是将 inetAddress 属性 放在数据库?

package infr.audio.model

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import infr.audio.messages.InetAddressWrapper
import java.io.Serializable
import java.net.InetAddress
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.NamedQueries
import javax.persistence.NamedQuery
import javax.persistence.Table

@Entity
@Table(name = "DIS_JOCKEY")
@NamedQueries(NamedQuery(name = "DisJockeyInfo.find", query = "SELECT m FROM DisJockeyInfo m"), NamedQuery(name = "DisJockeyInfo.findByReservationId", query = "SELECT m FROM DisJockeyInfo m WHERE m.reservationId = :reservationId"), NamedQuery(name = "DisJockeyInfo.findByReservationIdAndName", query = "SELECT m FROM DisJockeyInfo m WHERE m.reservationId = :reservationId AND m.name = :name"), NamedQuery(name = "DisJockeyInfo.findMaxDisEntityIdByReservationId", query = "SELECT MAX(m.disEntityId) FROM DisJockeyInfo m WHERE m.reservationId = :reservationId"), NamedQuery(name = "DisJockeyInfo.findActive", query = "SELECT m FROM DisJockeyInfo m WHERE m.disEntityId IS NOT NULL"), NamedQuery(name = "DisJockeyInfo.findActiveByInetAddress", query = "SELECT m FROM DisJockeyInfo m WHERE m.inetAddress = :inetAddress AND m.disEntityId IS NOT NULL"), NamedQuery(name = "DisJockeyInfo.findActiveByReservationId", query = "SELECT m FROM DisJockeyInfo m WHERE m.reservationId = :reservationId AND m.disEntityId IS NOT NULL"), NamedQuery(name = "DisJockeyInfo.deleteInactive", query = "DELETE FROM DisJockeyInfo m WHERE m.id = :id AND m.disEntityId IS NULL"))
data class DisJockeyInfo(
        @get:Id
        @get:GeneratedValue(strategy = GenerationType.IDENTITY)
        @get:Column(name = "ID")
        var id: Long,
        @get:Column(name = "RESERVATION_ID")
        var reservationId: String?,
        @get:Column(name = "NAME")
        var name: String?,
        @get:Column(name = "DIS_ENTITY_ID")
        var disEntityId: Long?,
        @get:java.beans.Transient
        @get:JsonIgnore
        @field:Transient
        var inetAddress: InetAddress?    
) : GenericEntity, Serializable {

    var inetAddressString: String?
        @Column(name = "INET_ADDRESS")
        @JsonProperty("inetAddress")
        get() = if (inetAddress != null) InetAddressWrapper(inetAddress).encodedInetAddress else null
        set(inetAddressString) {
            inetAddress = if (inetAddressString != null) InetAddressWrapper(inetAddressString).inetAddress else null
        }

    constructor() : this(id = 0L, reservationId = null, name = null, disEntityId = null, inetAddress = null) {}

    companion object {
        /** Serial version */
        private val serialVersionUID = 2027355982760360935L
    }
}

您正在注释字段而不是注释 getter。请注意,您的 getter 注释使用了错误的类型 (java.beans.Transient) 而不是正确的 javax.persistence.Transient.

只需添加:

import javax.persistence.Transient

使用@Transient 和导入kotlin.jvm.Transient 帮助了我。 (或者只是 @kotlin.jvm.Transienthttps://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-transient/