休眠:级联类型 [英] Hibernate: Cascade Type
问题描述
我们让实体 A
和实体 B
。实体 A
具有 @OneToOne
与 B
的关系。 >
我想要做下一个:
如果我删除A然后B也必须删除。
如果我删除B然后A不会被删除。
Let's I have entity A
and entity B
. Entity A
have @OneToOne
relationship with B
.
I want do next:
if I remove A then B must be removed also.
If I remove B then A isn't removed.
在哪个实体中,我必须设置
In which entity I must set
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
在哪一方面我必须设置
@OneToOne(cascade = {CascadeType.ALL})
?
?
推荐答案
从A到B的级联应放置在引用 B $ c $的字段上c>在类
A
中,从B到A的级联应放置在类<$ c中引用 A
的字段上
The cascade from A to B should be placed on the field referencing B
in class A
, the cascade from B to A should be placed on the field referencing A
in class B
.
public class A {
@OneToOne(cascade = {CascadeType.ALL})
B b;
}
应放在课程 A
,因为您希望每个操作都被级联到 B
。
Should be in class A
, as you want every action to be cascaded to B
.
public class B {
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
A a;
}
应放在课程 B
,因为您只希望将某些操作级联到 A
Should be in class B
, as you only want certain actions cascaded to A
这篇关于休眠:级联类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!