图扑物联 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?

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料