更新时间:2022-06-04 08:48:25
来自此处:-
级联删除
使用CascadeType.REMOVE(或CascadeType.ALL, (其中包括REMOVE)表示删除操作应为 自动级联到该对象所引用的实体对象 字段(集合可以引用多个实体对象 字段):
Marking a reference field with CascadeType.REMOVE (or CascadeType.ALL, which includes REMOVE) indicates that remove operations should be cascaded automatically to entity objects that are referenced by that field (multiple entity objects can be referenced by a collection field):
@Entity
class Employee {
:
@OneToOne(cascade=CascadeType.REMOVE)
private Address address;
:
}
移除孤儿
JPA 2支持其他更积极的删除级联模式 可以使用 @OneToOne和@OneToMany批注:
JPA 2 supports an additional and more aggressive remove cascading mode which can be specified using the orphanRemoval element of the @OneToOne and @OneToMany annotations:
@Entity
class Employee {
:
@OneToOne(orphanRemoval=true)
private Address address;
:
}
差异:-
这两个设置之间的区别在于对 断开关系.例如,当设置 地址字段设置为null或另一个Address对象.
The difference between the two settings is in the response to disconnecting a relationship. For example, such as when setting the address field to null or to another Address object.