[转载]hibernate经典问题a different object with the same identifier

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


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

这个错误我一共遇到过两次,一直没有找到很好的解决方案,这个错误产生原因相信大家都知道,因为在hibernate中同一个session里面有了两个相同标识但是是不同实体,当这时运行saveOrUpdate(object)操作的时候就会报这个错误。呵呵,也许你会说,你这么说跟没说没什么区别,我承认,呵呵,我不知道具体为什么会产生这个错误,要不然也不会很久都没有解决,现在,给出一个临时的解决方案,给向我一样,没有办法找到根源的人一个能够继续执行下去的方法(当然是对的,只是不是从产生原因入手)

其实要解决这个问题很简单,只需要进行session.clean()操作就可以解决了,但是你在clean操作后面又进行了saveOrUpdate(object)操作,有可能会报出"Found two representations of same collection",我找了很多资料,没有什么很好的解释,其中这篇文章帮助最大[url]http://opensource.atlassian.com/projects/hibernate/browse/HHH-509[/url]。

最后通过session.refresh(object)方法就可以解决了,注意,当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为refresh是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下

当然这个问题最容易解决的办法还是使用Hibernate里面自带的merge()方法。不过我始终觉得碰到问题就用这种软件自带的非常用方法(和saveOrUpdate(),save(),update()相比)感觉十分不爽。

后来我还发现这种错误经常出现在一对多映射和多对多映射,请大家在使用一对多和多对多映射的时候要小心一些


Hibernate 疑难异常及处理


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);
}
====================我是分割线===================其实我的解决办法是把obj给重新merge一下,注意红字部分public Serializable save(Object persistentObject) throws DaoException {
try {

Session session = this.openSession();
beginTransaction();
[color=red]persistentObject = session.merge(persistentObject);[/color]
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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值