HibernateException: Illegal attempt to associate a collection with two open sessions(解決方法)


在項目中遇到這個 HibernateException: Illegal attempt to associate a collection with two open sessions. 一查代碼發現是因為在service中存在兩個不同的hibernate session都同時引用了同一個collection對象,一個是load(),一個是saveOrUpdate(),簡單的修改方法: hibernate session 的 merge() 方法。hibernate3.0以上可以使用merge()來合並兩個session中的同一對。

將原來的代碼:

this.classificationDAO.saveOrUpdate(classification);

改成

this.classificationDAO.saveOrUpdate(this.classificationDAO.merge(classification));

這樣就解決問題了。


免責聲明!

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



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