hibernate異常”Found shared references to a collection

來源:互聯網
上載者:User
hibernate異常"Found shared references to a collection

問題:
假定,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.blogjava.net/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

http://www.blogjava.net/leekiang/archive/2008/10/31/237908.html

 轉自:http://blog.163.com/cjp1320@126/blog/static/3547436320092411404550/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.