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

相關文章
相關標籤/搜索