HibernateException: Illegal attempt to associat...

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

        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

解决方法

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

相关文章
相关标签/搜索