2017-05-30 104 views
0

我有一個實體類,我已經從Java遷移到Kotlin。其中一個屬性(inetAddress)使用@Transient進行註釋,以防止它被持久保存到關係數據庫中。但是,JPA(Hibernate實現)沒有看到這個註釋並將它放在數據庫中,因爲它是自己的字段。當這個類用Java編寫時,這一切都可以正常工作。Kotlin,JPA和@Transient

是否有人知道如何正確註釋這個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 
    } 
} 
+1

您註釋的是該字段而不是註釋吸氣劑。 –

+0

我還沒有使用Kotlin很長時間,但我相信我會在字段和getter上放置'@ Transient'註釋:'@get:java.beans.Transient'。這就是'@get:'部分的意思,不是? – Yoshiya

+0

除了正確的註釋不是java.beans.Transient,而是javax.persistence.Transient –

回答

2

您註釋的字段,而不是註釋吸氣劑。請注意,您的getter註釋使用不正確的類型(java.beans.Transient)而不是正確的javax.persistence.Transient