一對多關聯刪除多方報錯

在做信息和附件一對多關係關聯刪除的時候,遇到了問題,當刪除一個信息的附件的時候,信息的附件會自動的複製一份;而且這個自動複製的附件沒有對應的信息的編號就是外鍵;網上查瞭解決了問題;
當一對多刪除多方對象的時候,要先將要刪除的多方和一方先解除關係,然後才能刪除多方;這樣就不會報錯;
Action中的代碼
**********************************
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String pid =request.getParameter("pid");
String id =request.getParameter("id");
Post post = releaseService.findById(Integer.valueOf(pid));
//刪除附件
Attachment att = attService.getById(Integer.valueOf(id));
//先解除附件和信息之間的關係 然後再刪除 否則會報deleted object would be re-saved by cascade的錯誤
post.getAttachments().remove(att);
attService.delAtt(att);
request.setAttribute("post", post);
return mapping.findForward("show");
}
****************************************
信息的Post.hbm.xml 這個是一方 多方沒有特殊的配置
************
<!-- 一條信息對應多個附件 -->

<key column="pid"></key>
<one-to-many class="worklog.bean.Attachment"></one-to-many>

************
在刪除一方的時候多方將全部被刪除
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章