问题描述
我正在寻找文档或回答级联如何对child的child进行工作,例如:
I was looking for documentation or answer how will the cascade work for child of child , example :
public class Parent{
@OneToMany(fetch = FetchType.EAGER,mappedBy = "parent",cascade = CascadeType.ALL)
private List<Child> child;
}
public class Child{
@OneToMany(mappedBy="child")
private List<AnotherChild> anohterChild;
}
@ManyToOne
private Parent parent;
}
现在的问题是,父类中对子级"应用的级联运算是否会应用于"AnotherChild"?换句话说,如果我坚持父母"对象,它是否坚持"AnotherChild"?
now the question, will the cascade operation applied on "Child" from parent class apply to "AnotherChild" ?in other words if I persist "Parent" Object will it persist "AnotherChild" ?
推荐答案
如果您坚持自己的父母,则只有那些在您父母类的孩子列表中的孩子会被保留,而不是您孩子中的AnotherChilds列表,课.
If you persist your parent, only those childs which are in the child-list of your parent-class get persisted but not the list of AnotherChilds in your child-class.
如果您也想保留它们,也可以级联它:
If you wish to persist them too just cascade it too:
public class Child{
@OneToMany(mappedBy="child", cascade = CascadeType.PERSIST)
private List<AnotherChild> anohterChild;
@ManyToOne
private Parent parent;
}
并在真正需要时使用CascadeType.ALL
,因为这种级联类型不仅包含持久性.如下图所示,CascadeType.ALL
包括所有其他级联类型,包括级联类型"remove",这意味着,当删除父对象时,所有其他子对象也将被删除.
And just use CascadeType.ALL
when you really need it, because this cascade-type includes more than just persisting. As it is explained in the following picture, CascadeType.ALL
includes all other cascade-types including the cascade-type "remove" which means that, when your parent-object gets removed, all other child-objects get removed too.
这篇关于JPA Hibernate级联类型的孩子的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!