[英]Hibernate update one child in one to many
我是冬眠的新手,所以我不了解一些基本的知識。 我有實體A和實體B。這是一對多的關系。 因此,A可以有多個B。 下面是將新B添加到A時要保存的代碼。
A a= this.aService.getAById(AID);
b.setA(a);
a.getBSet().add(b);
this.aService.saveA(a);
但是如何編輯一個B實體? 我是否首先必須從集合中刪除要編輯的B實體? 真的很抱歉,如果這是一個明顯的問題。 但是我已經在Google搜索過,唯一可以找到的例子是創建新實體而不進行編輯時。
您需要首先從數據庫中獲取B。
B b = this.bService.getBById(BID);
...
//update b
this.bService.updateB(b);
//Whether you want update entity B:
Public void updateBEntity(Integer idB) {
B b = session.get(B.class, idB);
//For edit you only have to use the set's methods:
b.setName(anything);
b.setPosition(2);
//final y, that's all
session.merge(b);
}
//In your class controller or Action
有關合並/持久性的更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.