a different object with the same identifier value was already associated with the session。

來源:互聯網
上載者:User
[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)
以上兩中異常經常出現在一對多映射和多對多映射中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.