Hibernate PersistentSet remove()操作不起作用 [英] Hibernate PersistentSet remove() operation not working

查看:332
本文介绍了Hibernate PersistentSet remove()操作不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 父类{
@OneToMany(mappedBy = parent,cascade = CasacadeType.ALL)
Set< Child>儿童;
}

类子元素{
@Column(nullable = false)
@ManyToOne
父亲;





$ b现在,如果我在Set上执行remove()操作,它的元素实际上并没有被移除。

解决方案

映射应该如下所示:

  public class Parent {
@OneToMany(mappedBy = parent,cascade = CasacadeType.ALL,orphanRemoval = true)
Set< Child>儿童;

public void removeChild(Child child){
children.remove(child);
child.setParent(null);
}
}

public class Child {
@ManyToOne
父亲;
}

正如这篇文章,因为你有一个双向关联,你必须让双方同步。



因此,最好调用:

  parent.removeChild (儿童); 

这样, removeChild 即将删除来自 children Set的 Child ,并设置 Child 父母的关联。


I've a Set in my Parent entity as below:

Class Parent {
 @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL)
 Set<Child> children;
}

Class Child {
 @Column(nullable=false)
 @ManyToOne
 Parent parent;
}

Now event if I do a remove() operation on the Set for one of its element it doesn't actually get removed.

解决方案

Your mapping should look like this:

public class Parent { 
    @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL, orphanRemoval = true) 
    Set<Child> children;

    public void removeChild(Child child) {
        children.remove(child);
        child.setParent(null);
    }
}

public class Child {
    @ManyToOne
    Parent parent; 
}

As explained in this article, because you have a bidirectional association you have to have both sides in-sync.

Therefore, it's good practice to call:

parent.removeChild(child);

This way, removeChild is going to remove the Child from the children Set and also set the Child's parent association to null.

这篇关于Hibernate PersistentSet remove()操作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆