當前位置:
首頁 >
a different object with the same identifier val...
發(fā)布時間:2025/3/20
16
豆豆
生活随笔
收集整理的這篇文章主要介紹了
a different object with the same identifier val...
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
a different object with the same identifier value was already associated with the session一個經典的hibernate錯誤:a different object with the same identifier value was already associated with the session xxxx
hibernate3.0以上使用merge()來合并兩個session中的同一對象
具體到我自己的代碼就是
public Object getDomain(Object obj) {
? getHibernateTemplate().refresh(obj);
? return obj;
? }
? public void deleteDomain(Object obj) {
? obj = getHibernateTemplate().merge(obj);
? getHibernateTemplate().delete(obj);
? }
解決a different object with the same identifier value was already associated with the session錯誤?
這個錯誤我一共遇到過兩次,一直沒有找到很好的解決方案,這個錯誤產生原因相信大家都知道,因為在hibernate中同一個session里面有了兩個相同標識但是是不同實體,當這時運行saveOrUpdate(object)操作的時候就會報這個錯誤。呵呵,也許你會說,你這么說跟沒說沒什么區(qū)別,我承認,呵呵,我不知道具體為什么會產生這個錯誤,要不然也不會很久都沒有解決,現(xiàn)在,給出一個臨時的解決方案,給向我一樣,沒有辦法找到根源的人一個能夠繼續(xù)執(zhí)行下去的方法(當然是對的,只是不是從產生原因入手)
其實要解決這個問題很簡單,只需要進行session.clean()操作就可以解決了,但是你在clean操作后面又進行了saveOrUpdate(object)操作,有可能會報出"Found two representations of same collection",我找了很多資料,沒有什么很好的解釋,其中這篇文章幫助最大[url]http://opensource.atlassian.com/projects/hibernate/browse/HHH-509[/url]。
最后通過session.refresh(object)方法就可以解決了,注意,當object不是數據庫中已有數據的對象的時候,不能使用session.refresh(object)因為refresh是從hibernate的session中去重新取object,如果session中沒有這個對象,則會報錯所以當你使用saveOrUpdate(object)之前還需要判斷一下
當然這個問題最容易解決的辦法還是使用Hibernate里面自帶的merge()方法。不過我始終覺得碰到問題就用這種軟件自帶的非常用方法(和saveOrUpdate(),save(),update()相比)感覺十分不爽。
后來我還發(fā)現(xiàn)這種錯誤經常出現(xiàn)在一對多映射和多對多映射,請大家在使用一對多和多對多映射的時候要小心一些
Hibernate 疑難異常及處理
1、a different object with the same identifier value was already associated with the session。
錯誤原因:在hibernate中同一個session里面有了兩個相同標識但是是不同實體。
解決方法一:session.clean()
PS:如果在clean操作后面又進行了saveOrUpdate(object)等改變數據狀態(tài)的操作,有可能會報出"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)
以上兩中異常經常出現(xiàn)在一對多映射和多對多映射中
a different object with the same identifier value was already associated with the session
一個經典的hibernate錯誤:a different object with the same identifier value was already associated with the session xxxx
hibernate3.0以上使用merge()來合并兩個session中的同一對象
具體到我自己的代碼就是
public Object getDomain(Object obj) {
? getHibernateTemplate().refresh(obj);
? return obj;
? }
? public void deleteDomain(Object obj) {
? obj = getHibernateTemplate().merge(obj);
? getHibernateTemplate().delete(obj);
? }
====================我是分割線===================
其實我的解決辦法是把obj給重新merge一下,注意紅字部分 public Serializable save(Object persistentObject) throws DaoException {
? try {
? ? ?? ?
? Session session = this.openSession();
? beginTransaction();
? persistentObject = session.merge(persistentObject);
? Serializable id = session.save(persistentObject);
? if (autoCommit)
? commitTransaction();
? return id;
? } catch (HibernateException ex) {
? log.error("Fail to save persistentObject", ex);
? throw new DaoException("Fail to save persistentObject", ex);
? } finally {
? if (autoCloseSession)
? closeSession();
? }
? }
轉載于:https://my.oschina.net/u/617245/blog/71885
總結
以上是生活随笔為你收集整理的a different object with the same identifier val...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: easyUI 添加排序到datagrid
- 下一篇: 开源软件如何盈利