HibernateException: Illegal attempt to associat...

本文探讨了在使用Spring框架时遇到的HibernateException:Illegal attempt to associate a collection with two open sessions的问题。通过将session.update(entity)方法替换为session.merge(entity),解决了由于懒加载导致的错误。详细解释了错误原因及解决方案。

HibernateException: Illegal attempt to associate a collection with two open sessions

        Session session=getSession();
		Transaction tx = null;  
        try {  
            tx = session.beginTransaction();  
            session.update(user);  
            tx.commit();  
        } catch (Exception e) {  
            tx.rollback();  
            e.printStackTrace();  
        } finally {    
            session.close();  
        }

提示原因是使用开启了两个Session。
但是代码是一用,一提交,一关闭,应该不会开启两个Session的。 
后来发现又是因为懒加载的原因。

解决方法

session.update(entity)方法改成session.merge(entity),merge方法是合并为一个session。 

转载于:https://my.oschina.net/xing240/blog/174861

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值