我有一个Entity类,我已经从Java迁移到Kotlin。其中一个属性(inetAddress
)使用@Transient
进行注释,以防止它被持久化到关系数据库。但是,JPA(Hibernate实现)没有看到这个注释,而是将它作为自己的字段放在数据库中。当用Java编写类时,这一切都能正常工作。
有没有人知道如何正确注释这个Kotlin类,以便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
}
}
答案 0 :(得分:4)
您正在注释该字段而不是注释getter。请注意,您的getter注释使用了错误的类型(java.beans.Transient
),而不是正确的javax.persistence.Transient
。
答案 1 :(得分:0)
只需添加:
import javax.persistence.Transient
答案 2 :(得分:0)
使用@Transient 和 import kotlin.jvm.Transient 帮助了我。
(或只是@kotlin.jvm.Transient
)
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-transient/