a different object with the same identifier value was already associated with the session: 的解决方法

错误:

Struts has detected an unhandled exception:

Messages:
  1. a different object with the same identifier value was already associated with the session: [cn.wyu.bestHelp.domain.yy.Hospital#402802816343d093016343d155d10000]
  2. a different object with the same identifier value was already associated with the session: [cn.wyu.bestHelp.domain.yy.Hospital#402802816343d093016343d155d10000]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cn.wyu.bestHelp.domain.yy.Hospital#402802816343d093016343d155d10000]
    


    这个问题在我根据id去查我原本密码与新密码对比之后,再saveOrUpdate的时候就报错,错误就是session已经缓存了唯一id这个对象,因此就会出现这样的错误。

    在解决的过程中,看到了主要是把saveOrUpdate改为merge就噢了,里面涉及的是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)

以上两中异常经常出现在一对多映射和多对多映射中


参考资料点击打开链接



猜你喜欢

转载自blog.csdn.net/weixin_40295575/article/details/80518643
今日推荐