Issue
I have an Entity class that I have migrated from Java to Kotlin. One of the properties (inetAddress
) is annotated with @Transient
to prevent it from being persisted to the relational database. However, JPA (Hibernate implementation) does not see this annotation and places it in the database as it's own field. This all works properly when the class is written in Java.
Does anyone known how to properly annotate this Kotlin class so that JPA/Hibernate will see the @Transient
annotation and not place the inetAddress
property in the database?
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
}
}
Solution
You're annotating the field instead of annotating the getter. Note that your getter annotation uses an incorrect type (java.beans.Transient
) instead of the correct javax.persistence.Transient
.
Answered By - JB Nizet