Hibernate 在 saveOrUpdate() 一个 detached object 时候会报以下错误,
通常是 Web 页面修改和保存后出现
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [domain.Director#9]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [domain.Director#9]
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [domain.Director#9]
if (object.getId() != null) {
getSession().merge(object);
} else {
getHibernateTemplate().saveOrUpdate(object);
}
觉得这种方法不是很完美,寻找更好的解决方法
修改了Dao实现
本文探讨了在使用Hibernate框架时遇到的NonUniqueObjectException异常,该异常通常发生在尝试保存已与Session相关联的对象时。文章提供了一种通过判断对象ID是否为空来决定调用merge还是saveOrUpdate的方法,并寻求更优解决方案。
137

被折叠的 条评论
为什么被折叠?



