关于hibernate 更新或者删除报错different object with the same identifier

Hibernate 疑难异常及处理


一、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里面自带的方法,推荐使用。

二、Found two representations of same collection

  错误缘由:见1。

  解决方法:session.merge(object)

以上两中异常常常出如今一对多映射和多对多映射中数据库

相关文章
相关标签/搜索