這個錯誤我一共遇到過兩次,一直沒有找到很好的解決方案,這個錯誤產生原因相信大家都知道,因為在hibernate中同一個session里面有了兩個相同標識但是是不同實體.
一開始按網上說的用
session.merge(Object)報了一個錯,可能是沒有用好,改用 session.clear(); session.update(user);這樣就OK了,
方法為:
package org.springframework.orm.hibernate3.support;
...
public void modifyByMerge(User user) {
Session session = getHibernateTemplate().getSessionFactory().
getCurrentSession();
session.clear();
session.update(user);
}
...
項目用的是spring + hibernate所以得用getHibernateTemplate().getSessionFactory().getCurrentSession();得當前Session
posted on 2007-08-20 11:29
摩西 閱讀(29505)
評論(9) 編輯 收藏 所屬分類:
work_2007
Feedback
# re: 再次碰到:a different object with the same identifier value was already associated with the session
2009-11-06 13:26 |
檢查你類的 equal 方法 ,一般是不必用merge 什么的
回復 更多評論
# re: 再次碰到:a different object with the same identifier value was already associated with the session[未登錄]
2009-12-11 11:09 |
可能是因為在更新的時候,session中有這個對象,但是仍然new 一個對象出來,那樣saveorupdate的時候就會出來兩個對象
回復 更多評論
# re: 再次碰到:a different object with the same identifier value was already associated with the session
2010-06-30 15:18 |
@tanyb02@163.com
用了equals方法會怎樣,我發現我的就是那里有問題
回復 更多評論
# re: 再次碰到:a different object with the same identifier value was already associated with the session
2011-10-08 19:39 |
你內存中已經有一個實體類的對象,你現在可能又查找了一次,但是內存中有兩個相同的實體類就有所沖突了……
回復 更多評論
# re: 再次碰到:a different object with the same identifier value was already associated with the session
2013-03-15 14:56 |
項目中的session不能隨便clear吧。。
回復 更多評論
# re: 再次碰到:a different object with the same identifier value was already associated with the session
2014-04-08 18:05 |
好,支持。 我剛剛也遇到了這個問題
回復 更多評論
# re: 再次碰到:a different object with the same identifier value was already associated with the session[未登錄]
2014-07-25 17:57 |
同遇到這個問題.我找了好多辦法. 基本上出來的答案都是一致的..
但是我并不是你們那樣解決的. 出現這個問題的根本原因在與session中2個相同的實體對象. 如果能保證只有一個對象那么就不會出現這個問題.
比方說 你更新某個實體之前,查了一下這個實體,那么在更新就會報錯. 可以考慮用查出來的實體重新賦值. 在更新就不會報錯了
回復 更多評論
# re: 再次碰到:a different object with the same identifier value was already associated with the session
2014-08-25 08:25 |
在更新保存的時候,因為有不同的對象而發生了錯誤,在update之前用clear管用
回復 更多評論
# re: 再次碰到:a different object with the same identifier value was already associated with the session
2015-04-01 14:33 |
@test你的回答完美解決了我的問題 思路很正確
回復 更多評論