解决a different object with the same identifier value was already associated with the session错误


20:41:15

  今天做一个saveorupdate操作报错: 

 org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.xshcar.carcloud.entity.UboxTbl#1291]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.xshcar.carcloud.entity.UboxTbl#1291] 

一个经典的hibernate错误:a different object with the same identifier value was already associated with the session xxxx
hibernate3.0以上使用merge()来合并两个session中的同一对象  

解决:

在daoimpl层调用hibernate的getHibernateTemplate().merge(对象)方法;

public boolean executeUpdate(T t) {
        boolean b=true;
        try {
       t=(T)this.getHibernateTemplate().getSessionFactory().getCurrentSession().merge(t);
            getHibernateTemplate().update(t);
        } catch (DataAccessException e) {
            e.printStackTrace();
            b=false;
        }
            
        return b;
    }

 

 
 
 
 
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM