Neil的备忘录

just do it
posts - 66, comments - 8, trackbacks - 0, articles - 0

hibernate Exception

Posted on 2008-08-27 15:37 Neil's NoteBook 阅读(70) 评论(0)  编辑  收藏
a different object with the same identifier value was already associated with the session

因为在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();                 //important
        session.update(user);
    }
...
项目用的是spring + hibernate所以得用
getHibernateTemplate().getSessionFactory().getCurrentSession();得当前Session

只有注册用户登录后才能发表评论。


网站导航: