a different object with the same identifier value was already associated with the session

a different object with the same identifier value was already associated with the session
这个错误我一共遇到过两次,一直没有找到很好的解决方案,这个错误产生原因相信大家都知道,因为在hibernate中同一个session里面有了两个相同标识但是是不同实体.
一开始按网上说的用session.merge(Object)报了一个错,可能是没有用好,改用 session.clear(); session.update(user);这样就OK了,
方法为:

package  org.springframework.orm.hibernate3.support;

    
public   void  modifyByMerge(User user) {
        Session session 
=  getHibernateTemplate().getSessionFactory().
                          getCurrentSession();
        session.clear();
        session.update(user);
    }

项目用的是spring 
+  hibernate所以得用getHibernateTemplate().getSessionFactory().getCurrentSession();得当前Session

你可能感兴趣的:(a different object with the same identifier value was already associated with the session)