a different object with the same identifier value was already associated with th

最近在做批量更新的时候发现如下问题:
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.xxx.db.hibernate.mapping.OriginalRequireAttribute#2373]
	at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
	at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
	at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
	at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
	at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:768)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
	... 87 more



错误提示:
a different object with the same identifier value was already associated with   the session


原因:
因为在hibernate中同一个session里面有了两个相同标识但是是不同实体

解决方案:
1.
 Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
 session.clear();
 session.update(user);

2.
如果是struts+hibernate+spring框架下面有个很简单的方法:
getHibernateTemplate().merge(user)
hibernateTemplate.update(user);

你可能感兴趣的:(object)