【转】解决a different object with the same identifier value was already associated with the session错

转自:http://blog.sina.com.cn/s/blog_7f865faf0100w22x.html

如:更新一个信息。

如:把一个用户移出某个部门,根据部门的Id获取部门的信息,得到一个部门对象DepartmentInfo di,再Set set=di.getUsers();遍历该set如下:   

Iterator it=set.iterator();
   while(it.hasNext()){
    ui=(UserInfo) it.next();
    if(ui.getId()==id){
          ui.getDepartments().remove(di);
          di.getUsers().remove(ui);
    }
   }

   session.update(di);
   session.update(ui);

 

思路:必须要从部门信息中获取用户的对象。

误区:不能new一个用户对象然后根据用户Id获取用户信息再赋给这个对象,这样的话就会产生a different object with the same identifier value was already associated with the session错误了!!!

posted @ 2016-07-27 09:37  GoQC  阅读(164)  评论(0)    收藏  举报