讓說我有下面的實體控制子實體
@Entity
public class Item{
private Country origin;
@ManyToOne(optional=true)
@JoinColumn(name="origin")
public Country getOrigin() {
return this.origin;
}
}
@Entity
public class Country{
private String code;
private String desc;
@Id
public String getCode() {
return this.code;
}
@Column
public String getDesc() {
return this.desc;
}
}
其中Country
類映射到一個預設的主表,不應該被修改。 用戶可以在創建新的Item
時設置origin
的code
。 如何檢查提供的code
是否已經是主表中的條目?
如果我只是persist()
一個新的Item
與不存在的code
沒有堅持孩子,org.hibernate.TransientObjectException
將被報告。我是否需要嘗試捕獲此異常以將其視爲不存在的主數據?或者有更好的選擇嗎?
在實際情況下,Item
對象中有很多這些子實體,我認爲逐個明確查找主數據並不是一個好主意。我認爲這也不是一個好的解決方案。