hibernate 一對多 級聯 保存修改 刪除


一對多,一端設置:

  <set name="TWorkorderHistories" inverse="true" cascade="all">

一)保存級聯

  問題:

    一方主鍵數據庫中沒有,先保存多方時,回報引用暫時態對象錯誤,

  分析:

    先不關聯多方地保存一方,

  解決:

    假設一方A,多方B

    1)B.setA(A);

    2)A.getSetB().add(B);

    3)session.save(A);

  或:

 1 // 插入記錄
 2         session.saveOrUpdate(workorder);
 3         tr.commit();
 4         session.close();
 5         
 6         session = hibernateDao.getHSession();
 7         tr = session.beginTransaction();
 8         // 添加工單歷史
 9         TWorkorderHistory tWorkorderHistory = new TWorkorderHistory(workorder,
10                 pureUser, 1L, new Timestamp(new Date().getTime()), Short
11                         .parseShort("0"));
12 
13         session.saveOrUpdate(tWorkorderHistory);
14         workorder.getTWorkorderHistories().add(tWorkorderHistory);
15 
16         // 插入記錄
17         tr.commit();
18         session.close();
保存級聯

 

二)修改級聯

  問題一:

  org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions

  解決:

    參考:http://blog.163.com/chinagujing@yeah/blog/static/170937439201092405558777/

       http://littie1987.iteye.com/blog/1039082

 1 @Override
 2     public void update(TWorkorderInfo workorder,User pureUser, String note, Short status)
 3             throws DataAccessException {
 4         
 5         
 6         
 7         workorder.setStatus(status);
 8         workorder.setUpdated(new Timestamp(new Date().getTime()));
 9         
10         
11         //開啟事務
12         Session session = hibernateDao.getHSession(); 
13         Transaction tr = session.beginTransaction();
14         
15         
16         TWorkorderHistory tWorkorderHistory = new TWorkorderHistory();
17         tWorkorderHistory.setTWorkorderInfo(workorder);
18         tWorkorderHistory.setCreated(new Timestamp(new Date().getTime()));
19         tWorkorderHistory.setOptType(status);
20         tWorkorderHistory.setNote(note);
21         tWorkorderHistory.setPureUser(pureUser);
22         tWorkorderHistory.setWorkflowId(1L);
23         
24         session.saveOrUpdate(tWorkorderHistory);
25         workorder.getTWorkorderHistories().add(tWorkorderHistory);
26         
27         
28         session.merge(workorder);
29         tr.commit();
30         session.close();
31     }
更新級聯

 

  問題二:

  org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.bonc.pure.domain.User

  分析:

    前台忘記傳這個User了,一共有兩個這樣的User對象,后台只set了一個,前台傳的是workorder.pureUserByModifier.userName,

    struts給new的這個User對象只有userName一個屬性,沒有userId,而主鍵關聯是需要通過userId的

  解決:

    <s:hidden name="workorder.pureUserByModifier.userId"></s:hidden>

二) 刪除級聯

  問題:

  org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions

  解決:    

    參考:http://www.itzhai.com/hibernate-one-to-many-association-mapping-configuration-and-the-cascade-delete-problem.html

1 Session session = hibernateDao.getHSession();
2             session.beginTransaction();
3             TWorkorderInfo workorder = (TWorkorderInfo) session.load(clazz, workorderCode);
4             session.delete(workorder);
5             session.beginTransaction().commit();
刪除級聯

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM