Posted on 2008-10-31 22:11
leekiang 閱讀(7665)
評論(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.tkk7.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