1 2 |
<!-- lang: java --> org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; |
错误原因分析: 因为Hibernate默认是只允许单个session存在,如果有两个session同时open,并同一个collection进行操作,Hibernate是无法判断使用那个。 在wel.xml文件中配置了opensession Filter,设置为singleSession。
1 2 3 4 5 6 7 8 9 |
<!-- lang: xml --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> |
但是如果我们想通过改变singleSession为false解决问题,我个人认为这个是不合理做法,还是该想其他解决方法。我提供一个解决方法供大家参考。
解决方法: 而且我通过google的搜索的时候,发现这个问题经常出现在1:N的情况下。所以在这种情况下要更为注意open two session这个问题,以免浪费必要的时间在解决错误上。
1、将两个同时对数据库操作的(update/save)法写在一个事务中。
2、将Hibernate的update方法改为merge。