gpt4 book ai didi

hibernate - Kotlin、JPA 和@Transient

转载 作者:行者123 更新时间:2023-12-02 13:09:32 26 4
gpt4 key购买 nike

我有一个从 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
}
}

最佳答案

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

关于hibernate - Kotlin、JPA 和@Transient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com