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
          異常是可以解決,卻更新了不要更新的數據
          主站蜘蛛池模板: 金平| 普安县| 康平县| 龙南县| 乐清市| 静宁县| 柘城县| 若尔盖县| 高州市| 宁夏| 富阳市| 县级市| 南涧| 离岛区| 江孜县| 富宁县| 邵阳市| 岳西县| 乌兰浩特市| 新龙县| 沂水县| 准格尔旗| 吴忠市| 福州市| 滦平县| 枣阳市| 林西县| 大渡口区| 志丹县| 宁安市| 云梦县| 贵定县| 清水河县| 新邵县| 成都市| 西乡县| 静宁县| 庆阳市| 那曲县| 景泰县| 张掖市|