Hibernate:a different object with the same identifier value was already associated with the session

来源:互联网 发布:程序员驿站 编辑:程序博客网 时间:2024/05/17 07:45

Hibernate异常:a different object with the same identifier value was already associated with the session
【出错原因】上网查了半天,据说这个异常很经典,很久之前就存在。此异常是在修改对象然后更新到表的时候发生的。异常字面意思是:session中已经存在一个与当前对象不同但是标识符相同的对象。
【具体环境背景】关键代码。
实体中代码:

@Entitypublic class Department {    private Long id;    private Set<User> users = new HashSet<User>();    private Department parent;    private Set<Department> children = new HashSet<Department>();    private String name;    private String description;    @Id    @GeneratedValue    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    @ManyToOne(cascade=CascadeType.ALL)    @JoinColumn(name="parent_id")    public Department getParent() {        return parent;    }    public void setParent(Department parent) {        this.parent = parent;    }    @OneToMany(mappedBy="parent",cascade=CascadeType.REMOVE,fetch=FetchType.EAGER)    public Set<Department> getChildren() {        return children;    }    public void setChildren(Set<Department> children) {        this.children = children;    }}

Action中代码:

    //修改界面(回显数据)    public String editUI() throws Exception {        // TODO Auto-generated method stub        Department department=departmentService.getById(model.getId());        ActionContext.getContext().getValueStack().push(department);        //savaUI的上级部门数据回显,首先回显list        List<Department> list=departmentService.findAll();        ActionContext.getContext().put("list", list);        //设置值,其次让界面回显上级部门        if(department.getParent()!=null){            parentId=department.getParent().getId();          }        return "saveUI";    }    //修改操作    public String edit() throws Exception {        // TODO Auto-generated method stub        Department department=departmentService.getById(model.getId());        department.setName(model.getName());        department.setDescription(model.getDescription());        //给修改后的部门设置上级部门        department.setParent(departmentService.getById(parentId));        departmentService.update(department);        return "toList";    }

无关代码已删除。
需求是,点击“修改”超链接,跳到修改页面(实际上是一个回显数据表单页面),修改完之后,点击“提交”超链接,发生这个异常a different object with the same identifier value was already associated with the session。
网上有好多解决办法:
1、a different object with the same identifier value was already associated with the session。
  错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。
  解决方法一:session.clean()
  PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出”Found two representations of same collection”异常。
  解决方法二:session.refresh(object)
  PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。
  解决方法三:session.merge(object)
  PS:Hibernate里面自带的方法,推荐使用。
2、Found two representations of same collection
  错误原因:见1。
  解决方法:session.merge(object)
以上两中异常经常出现在一对多映射和多对多映射中。
所有的方法都尝试了,在我这都没有用。
【解决办法】把@ManyToOne中的cascade=CascadeType.ALL改成ascade=CascadeType.MERGE或者直接删掉,只用@ManyToOne。

0 0