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