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