从DB中删除cascade和orphan有什么区别? [英] What is the difference between cascade and orphan removal from DB?

查看:455
本文介绍了从DB中删除cascade和orphan有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

@OneToMany(cascade=REMOVE, mappedBy="customer")
public List<Order> getOrders() { ... }

@OneToMany(mappedBy="customer", orphanRemoval="true")
public List<Order> getOrders() { ... }

此示例来自Java EE Tutorial,但我仍然不喜欢了解详细信息。

This example is from Java EE Tutorial, but I still don't understand details.

推荐答案

来自此处: -


级联删除

使用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注释的orphanRemoval元素指定:

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;
     :
}

DIFFERENCE: -

两个设置之间的区别在于对
断开关系的响应。例如,例如将
地址字段设置为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.


  • 如果 orphanRemoval =指定true ,自动删除已断开连接的Address实例。这对于清理
    依赖对象(例如地址)非常有用,这些对象在没有所有者对象(例如Employee)的
    引用时不应该存在。

  • 如果只是<指定strong> cascade = CascadeType.REMOVE 不会执行自动操作,因为断开关系不是删除

    操作。

  • If orphanRemoval=true is specified the disconnected Address instance is automatically removed. This is useful for cleaning up dependent objects (e.g. Address) that should not exist without a reference from an owner object (e.g. Employee).
  • If only cascade=CascadeType.REMOVE is specified no automatic action is taken since disconnecting a relationship is not a remove
    operation.

这篇关于从DB中删除cascade和orphan有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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