s2sh保存错误: a different object with the same identifier value was already associated


org.springframework.orm.hibernate3.HibernateSystemException:
          a different object with the same identifier value was already associated with the session: [com.demo.bean.Option]

因为在hibernate中同一个session里面有了两个相 同标识但是是不同实体,当这时运行saveOrUpdate(object)操作的时候就会报这个错误。解决方法是:
hibernate3.0以上使用merge()来合并两个session中的同一对象:
public void attachDirty(Role instance) {
        log.debug("attaching dirty Role instance");
        try {
           instance = hibernateTemplate.merge(instance);
            hibernateTemplate.saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

原文地址:https://www.cnblogs.com/summer520/p/2964263.html