Hibernate:a different object with the same identifier value was already associated with the session
2013-07-22 16:38
501 查看
再次遇到a different object with the same identifier value was already associated with the session错误
这错误以前遇到过一次,我是这样解决的:正常情况一般调用hiberate端方法进行数据库操作时,是直接调用 getHibernateTemplate().update(user);,但在hibernate中同一个session里面有了两个相同标识但是是 不同实体时就会出现如标题的错误,在网上找来如下解决方法,错误是解决了,但不完美:
public void update(User user) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.clear();
session.update(user);
}
这 样在session.update(user)后如果还有其他对数据库的save或update并且是在同一个事务中,后面的操作可能就会无效,具体原因 没查出来,可能是getcurrentSession()获取的是“当前”session,调用getcurrentSession()时“当前 ”session被从hibernate中剥离,所以导致了后面的操作无效。
如果是struts+hibernate+spring框架下面有个很简单的方法:直接调用getHibernateTemplate().merge(user)。一切搞定!
http://www.havenliu.com/java/12.html
相关文章推荐
- hibernate中一种导致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 错误
- hibernate: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
- Hibernate更新数据报错:a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]
- HibernateSystemException: a different object with the same identifier value was already associated with the session.
- a different object with the same identifier value was already associated with the session 一个经典的hibernate错误
- Hibernate Error: 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
- hibernate 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
- hibernate a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session错误
- a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session错误
- a different object with the same identifier value was already associated with the session的解决
- different object with the same identifier value was already associated with the session 问题解决
- a different object with the same identifier value was already associated with the session