[英]Hibernate One to One mapping not updating the child table
我有一對一映射的關聯類。
@Entity
public class EmployeeEntity
{
@Id
private String id;
private String name;
@OneToOne(mappedBy = "employeeEntity", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "empid")
private AddressEntity addressEntity;
...
...
getters & setters
}
@Entity
public class AddressEntity
{
@Id
@Column(unique=true, nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="employeeEntity"))
private String empId;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private EmployeeEntity employeeEntity;
...
getters & setters
}
我正在使用postgres並具有表(員工身份,地址實體),並在地址實體表上具有以下foriegn鍵約束:外鍵約束:“ fkakhilesh”外鍵(empid)引用employeeentity(id)在刪除級聯上
我對不同的REST調用有以下要求:
我能夠執行1到5的操作而沒有任何問題。
主要問題是在6中,以下是我想到的問題:1.當我執行“ getSession()。update(object)”時,我得到了Hibernate的StaleStateException:批處理更新從更新[0]返回了意外的行數; 實際行數:0; 預期:1.如果地址不存在,使用“更新”是不可能的嗎? 更新期間不能創建新地址嗎?
我是否需要將ServiceImpl調用更改為“ getSession()。merge(object)”?這種情況只能通過調用“ merge”來處理嗎?它如何影響性能?
如果我確實合並,則會得到休眠的IdentifierGenerationException:嘗試從空的一對一屬性分配ID。 我在這里想念什么嗎?
這可以通過更改休眠映射來解決? 或與級聯有關的東西。
@GeneratedValue(generator =“ gen”)在這里的重要性是什么? 為什么在@GenericGenerator中使用@parameter
我是休眠的新手,正在嘗試進入休眠映射的深度。 另外,如果您能在設計上提出建議,我將非常高興,因為這應該是處理此問題的最佳方法。
我已經解決了。 這種一一對應的映射有些棘手,並不像我最初想象的那么簡單。 我已經使用了雙向的一對一映射,因此在更新過程中調用EmployeeEntity和AddressEntity的設置器互相設置很重要。 例如:
employeeEntity.setAddressEntity(addressEntity)和addressEntity.setEmpoyeeEntity(empoyeeEntity)必須顯式調用。
單獨設置employeeEntity.setAddressEntity(addressEntity)將不起作用。
始終使用整數ID並使用.getSession.saveOrUpdate(entity);
用於保存或更新。
在一對一映射中,您應該在孩子上提到constrained=true
。 它使子ID與父ID相同。
將這些行用作子ID。 我不知道Java屬性語法。
<generator class="foreign">
<param name="property">employeeEntity</param>
</generator>
Cascade.All
。 我認為默認的獲取模式是Select
,這很好。 級聯通常用於父部分,該部分負責父子關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.