1、a different object with the same identifier value was already associated with the session。
错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。
解决方法一:session.clean()
PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。
解决方法二:session.refresh(object)
PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。
解决方法三:session.merge(object)
PS:Hibernate里面自带的方法,推荐使用。
2、Found two representations of same collection
错误原因:见1。
解决方法:session.merge(object)
以上两中异常经常出现在一对多映射和多对多映射中
a different object with the same identifier value was already associated with the session
一个经典的hibernate错误:a different object with the same identifier value was already associated with the session xxxx
hibernate3.0以上使用merge()来合并两个session中的同一对象
具体到我自己的代码就是
public Object getDomain(Object obj) {
getHibernateTemplate().refresh(obj);
return obj;
}
public void deleteDomain(Object obj) {
obj = getHibernateTemplate().merge(obj);
getHibernateTemplate().delete(obj);
}
错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。
解决方法一:session.clean()
PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。
解决方法二:session.refresh(object)
PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。
解决方法三:session.merge(object)
PS:Hibernate里面自带的方法,推荐使用。
2、Found two representations of same collection
错误原因:见1。
解决方法:session.merge(object)
以上两中异常经常出现在一对多映射和多对多映射中
a different object with the same identifier value was already associated with the session
一个经典的hibernate错误:a different object with the same identifier value was already associated with the session xxxx
hibernate3.0以上使用merge()来合并两个session中的同一对象
具体到我自己的代码就是
public Object getDomain(Object obj) {
getHibernateTemplate().refresh(obj);
return obj;
}
public void deleteDomain(Object obj) {
obj = getHibernateTemplate().merge(obj);
getHibernateTemplate().delete(obj);
}
====================我是分割线===================
其实我的解决办法是把obj给重新merge一下,注意红字部分
public Serializable save(Object persistentObject) throws DaoException {
try {
Session session = this.openSession();
beginTransaction();
persistentObject = session.merge(persistentObject);
Serializable id = session.save(persistentObject);
if (autoCommit)
commitTransaction();
return id;
} catch (HibernateException ex) {
log.error("Fail to save persistentObject", ex);
throw new DaoException("Fail to save persistentObject", ex);
} finally {
if (autoCloseSession)
closeSession();
}
}
try {
Session session = this.openSession();
beginTransaction();
persistentObject = session.merge(persistentObject);
Serializable id = session.save(persistentObject);
if (autoCommit)
commitTransaction();
return id;
} catch (HibernateException ex) {
log.error("Fail to save persistentObject", ex);
throw new DaoException("Fail to save persistentObject", ex);
} finally {
if (autoCloseSession)
closeSession();
}
}