Hibernate exception - Illegal attempt to associate a collection with two open session
采用 struts2 + hibernate3.3 + spring2.5.6
<!-- 由spring控制hibernate的session的打开与关闭 --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>false</param-value> </init-param> </filter>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事务处理 --> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.dlut.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="addOrUpdate*" rollback-for="Exception" /> <tx:method name="add*" rollback-for="Exception" /> <tx:method name="update*" rollback-for="Exception" /> <tx:method name="delete*" rollback-for="Exception" /> </tx:attributes> </tx:advice>
Site site = siteService.getSiteByName("site01"); if (site == null) { site = new Site(); } site.setName(item.getName()); siteService.addOrUpdateSite(site);
public void addOrUpdateSite(Site site) { siteDao.saveOrUpdate(site); }
public void saveOrUpdate(Site site) { getHibernateTemplate().saveOrUpdate(site); }
只要是执行update或者saveOrUpdate方法就会抛以上异常,执行insert是没有问题的.
有人说我的事务处理没做好,我做了一下调试,发现没有问题, open session in view filter里面默认的FlushMode=NEVER,当执行到addOrUpdate*方法时,spring自动将FlushMode设为AUTO,当执行到"doInHibernate()"方法的时候抛出了以上异常,但是我调试的时候看到session只是打开了一个,好几天了,一直没想到怎么解决,非常的郁闷.
问题补充:
<param-name>singleSession</param-name>
<param-value>false</param-value>
以前用的是true,因为出了那个异常,实在没办法了,又改成了false试了试,后来忘改回来了,谢谢提醒.先把分给你吧,我自己再检查一下