我在兩個表之間有一對一的映射:
人-> 身份證| 姓名
person_status -> id | 人名 | 地位
我需要一個單向映射,即我想從 person 物件中獲取 personStatus,但不是相反。
以下是物體:
@Entity(name = "Person")
class PersonEntity(
@Id
@Column(name = "id", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
val name: String,
@OneToOne(mappedBy = "person", cascade = [CascadeType.ALL])
var personStatus: PersonStatusEntity,
)
@Entity(name = "PersonStatus")
class PersonStatusEntity(
@Id
@Column(name = "id", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
@OneToOne(cascade = [CascadeType.ALL])
@JoinColumn(name = "person_id", referencedColumnName = "id")
var person: PersonEntity? = null,
@Enumerated(EnumType.STRING)
val status: Status,
@UpdateTimestamp
val updateTime: OffsetDateTime? = null
)
現在,當我運行時personRepository.findOneByName("John"),我得到了 PersonEntity 物件,其中填充了 personStatus 欄位。但是在那個 personStatus 物件中,這個 person 物件是空的。
personEntity =
-- id = 1
-- name = "John"
-- personStatus =
----- id = 1
----- person = null
----- status = APPROVED
----- updateTime = "2022-10-14T10:50:21.519228 11:00"
理想情況下,personStatus 中的這個 person 欄位不應該存在,因為我不想從 personStatus 物件訪問一個人。但是我無法找出正確的注釋來告訴休眠在加入時要查找的列。如果沒有辦法做到這一點,那么至少它不應該為空。我查看了一些現有的問題,但他們正在做雙向映射,我試圖避免這種情況。我怎樣才能做到這一點?謝謝。
uj5u.com熱心網友回復:
為什么需要單獨的主鍵PersonStatus?改為這樣建模:
@Entity(name = "Person")
class PersonEntity(
@Id
@Column(name = "id", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
val name: String,
@OneToOne(cascade = [CascadeType.ALL], orphanRemoval = true)
@JoinColumn(name = "id", referenceColumnName = "person_id")
var personStatus: PersonStatusEntity,
)
@Entity(name = "PersonStatus")
class PersonStatusEntity(
@Enumerated(EnumType.STRING)
val status: Status,
@UpdateTimestamp
val updateTime: OffsetDateTime? = null
)
甚至更好,通過輔助表:
@Entity(name = "Person")
@SecondaryTable(name = "PersonStatus", pkJoinColumns = @PrimaryKeyJoinColumn(name = "person_id"))
class PersonEntity(
@Id
@Column(name = "id", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
val name: String,
@Embedded
var personStatus: PersonStatus,
)
@Embeddable
class PersonStatusEntity(
@Column(table = "PersonStatus")
@Enumerated(EnumType.STRING)
val status: Status,
@Column(table = "PersonStatus")
@UpdateTimestamp
val updateTime: OffsetDateTime? = null
)
也許你需要行內這個:
@Entity(name = "Person")
@SecondaryTable(name = "PersonStatus", pkJoinColumns = @PrimaryKeyJoinColumn(name = "person_id"))
class PersonEntity(
@Id
@Column(name = "id", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
val name: String,
@Column(table = "PersonStatus")
@Enumerated(EnumType.STRING)
val status: Status,
@Column(table = "PersonStatus")
@UpdateTimestamp
val updateTime: OffsetDateTime? = null
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/521227.html
上一篇:java.lang.IllegalArgumentException:找不到標頭名稱“first_name”。可用列
