[DH-OFFICE] 2013-06-16 23:50:30 ERROR [http-8080-5] NewsAction.save(128) | a different object with the same identifier value was already associated with the session: [com.sencloud.dh.oa.model.info.News#10060]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.sencloud.dh.oa.model.info.News#10060]
今天遇到這個問題: 可以通過下面的方式解決。
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)
以上兩中異常經常出現在一對多映射和多對多映射中