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; }