图扑物联 2009-05-18 10:14
浏览 223
已采纳

Hibernate 异常之:associate a collection with two ...

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试了试,后来忘改回来了,谢谢提醒.先把分给你吧,我自己再检查一下
  • 写回答

1条回答 默认 最新

  • Vincent2013SH 2009-05-18 10:14
    关注

    singleSession

    false

    为何用false?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能