zoukankan      html  css  js  c++  java
  • Hibernate Error: a different object with the same identifier value was already associated with the session

    在执行Hibernate的Update操作时,报错:a different object with the same identifier value was already associated with the session。

    原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。通常来说,使用session的merge(object)便可以解决。

    但也有其他令几种方式供参考:

    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)

     

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

     

     

     

  • 相关阅读:
    数据仓库经验小结(转)
    SSIS高级内容 系列一
    SSIS高级内容 系列二
    SSIS数据流组件开发(1)转载
    在sharepoint中添加视频播放
    处理数据仓库中历史数据的若干问题
    SSIS数据流组件开发(2) 转载
    [转载]俄罗斯只剩“大国的脾气”
    [转载]游击队员之歌
    [转载]世界是女人的
  • 原文地址:https://www.cnblogs.com/jaenson/p/3375751.html
Copyright © 2011-2022 走看看