在項目中遇到這個 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));
這樣就解決問題了。