a different object with the same identifier val...

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)操做的時候就會報這個錯誤。呵呵,也許你會說,你這麼說跟沒說沒什麼區別,我認可,呵呵,我不知道具體爲何會產生這個錯誤,要否則也不會好久都沒有解決,如今,給出一個臨時的解決方案,給向我同樣,沒有辦法找到根源的人一個可以繼續執行下去的方法(固然是對的,只是否是從產生緣由入手)

其實要解決這個問題很簡單,只須要進行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()相比)感受十分不爽。

後來我還發現這種錯誤常常出如今一對多映射和多對多映射,請你們在使用一對多和多對多映射的時候要當心一些


Hibernate 疑難異常及處理


一、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裏面自帶的方法,推薦使用。

二、Found two representations of same collection

  錯誤緣由:見1。

  解決方法:session.merge(object)

以上兩中異常常常出如今一對多映射和多對多映射中


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();   }   }
相關文章
相關標籤/搜索