MDA/MDD/TDD/DDD/DDDDDDD
          posts - 536, comments - 111, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          hibernate異常"Found shared references to a collection"

          Posted on 2008-10-31 22:11 leekiang 閱讀(7677) 評論(2)  編輯  收藏 所屬分類: hibernate
          問題:
          假定,Parent類有一個Set屬性,里面放的是Son。如果查詢"from Parent",某個Parent哪怕一個Son都沒有,那個Set屬性不會為null,而是一個空集合。
          這時候如果你Parent newP=new Parent();然后BeanUtils.copyPropertis(newP,origP);最后就會報hibernate異常"Found shared references to a collection"。
          注:hibernate在什么時機發現"兩個對象共享一個集合"的情況的?我這邊的例子是在下一次查詢時發現的。

          原因:
          BeanUtils.copyPropertis是淺拷貝,導致這兩個對象引用的Set是同一個Set,這在hibernate中是不允許的,參見Hibernate reference第6章的"Two entities may not share a reference to the same collection instance"。
          這種問題常見于復制對象時。
          如何解決:newP.setSonSet(null);
          還有人說原因可能是并發操作:http://www.aygfsteel.com/fastzch/archive/2006/12/22/89520.html

          參考:
          http://markmail.org/message/fszouomkeicjynw2
          http://blog.csdn.net/programeyonger/archive/2008/01/31/2075304.aspx
          http://www.javaeye.com/topic/99505

          評論

          # re: hibernate異常"Found shared references to a collection"[未登錄]  回復  更多評論   

          2008-12-25 15:02 by 流浪者
          你說的太對了 厲害

          # re: hibernate異常"Found shared references to a collection"  回復  更多評論   

          2010-06-08 15:18 by likei
          異常是可以解決,卻更新了不要更新的數據
          主站蜘蛛池模板: 库伦旗| 井陉县| 颍上县| 凯里市| 临湘市| 来安县| 锡林郭勒盟| 江孜县| 本溪市| 石台县| 惠来县| 三江| SHOW| 北碚区| 班戈县| 凯里市| 昂仁县| 忻城县| 德兴市| 东宁县| 耿马| 布拖县| 龙游县| 沙湾县| 海口市| 东平县| 林口县| 兰坪| 四会市| 东乌珠穆沁旗| 小金县| 明星| 北流市| 石河子市| 瑞安市| 理塘县| 西乌珠穆沁旗| 株洲市| 绥德县| 阿图什市| 南京市|