Java 从数据库中级联删除和孤立删除有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18813341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:17:20  来源:igfitidea点击:

What is the difference between cascade and orphan removal from DB?

javajpacascading-deletes

提问by rand0m86

What's the difference between

有什么区别

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

and

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

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

这个例子来自Java EE Tutorial,但我还是不明白细节。

采纳答案by Rahul Tripathi

From here:-

这里:-

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

Orphan Removal

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

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.

  • If orphanRemoval=trueis 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.REMOVEis specified no automatic action is taken since disconnecting a relationship is not a remove
    operation.

级联删除

用 CascadeType.REMOVE(或 CascadeType.ALL,包括 REMOVE)标记引用字段表示移除操作应该自动级联到该字段引用的实体对象(多个实体对象可以被一个集合字段引用):

@Entity
class Employee {
     :
    @OneToOne(cascade=CascadeType.REMOVE)
    private Address address;
     :
}

孤儿移除

JPA 2 支持额外的、更积极的删除级联模式,可以使用 @OneToOne 和 @OneToMany 注释的 orphanRemoval 元素指定:

@Entity
class Employee {
     :
    @OneToOne(orphanRemoval=true)
    private Address address;
     :
}

区别:-

两种设置之间的区别在于对断开关系的响应。例如,当将地址字段设置为 null 或另一个地址对象时。

  • 如果指定了orphanRemoval=true,则自动删除断开连接的 Address 实例。这对于清理在没有所有者对象(例如员工)的引用的情况下不应该存在的依赖对象(例如地址)很有用。
  • 如果仅指定了cascade=CascadeType.REMOVE,则不会采取自动操作,因为断开关系不是删除
    操作。

回答by study

An easy way to understand the difference between CascadeType.REMOVEand orphanRemoval=true.

一个简单的方法来理解之间的差异CascadeType.REMOVEorphanRemoval=true

For orphan removal: If you invoke setOrders(null), the related Orderentities will be removed in db automatically.

对于孤儿移除:如果您调用setOrders(null),相关Order实体将在 db 中自动移除。

For remove cascade: If you invoke setOrders(null), the related Orderentities will NOTbe removed in db automatically.

对于删除级联:如果您调用setOrders(null)Order不会自动删除数据库中的相关实体。

回答by Mr.Q

Suppose we have a child entity and a parent entity. A parent can have several children.

假设我们有一个子实体和一个父实体。一个父母可以有几个孩子。

@Entity
class parent {
  //id and other fields
 @OneToMany (orphanRemoval = "true",cascade = CascadeType.REMOVE)
   Set<Person> myChildern;
}

The orphanRemoval is an ORM concept, it tells if the child is orphaned. it should also be removed from the database.

orphanRemoval 是一个 ORM 概念,它告诉孩子是否是孤儿。它也应该从数据库中删除。

A child is orphaned when it can`t be accessed from its parent. For example, if we remove the Person objects set (setting it to an empty set) or replace it with a new set then the parent can no longer access the children in the old set and the children are orphaned so the children are doomed to be removed in the database also.

当孩子无法从其父级访问时,它就是孤儿。例如,如果我们删除 Person 对象集(将其设置为空集)或将其替换为新集,那么父对象将无法再访问旧集合中的子对象,并且子对象是孤立的,因此子对象注定在数据库中也删除。

CascadeType.REMOVE is a database level concept and it tells if the parent is removed, all its related records in the child table should be removed.

CascadeType.REMOVE 是一个数据库级别的概念,它告诉如果父表被删除,它在子表中的所有相关记录都应该被删除。

回答by garg10may

Practically the difference lies in whether you are trying to update the data (PATCH) or entirely replace the data (PUT)

实际上区别在于您是尝试更新数据(PATCH)还是完全替换数据(PUT)

Let's say you delete the customerthan using cascade=REMOVEwill also remove that customers orders which seem intended and useful.

假设您删除customerthan usingcascade=REMOVE也将删除那些看起来有意且有用的客户订单。

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

Now let's say you update a customerwith orphanRemoval="true"it will delete all previous orders and replace them with the one provided. (PUTin terms of REST API)

现在让我们假设你更新customerorphanRemoval="true"它会删除所有以前的订单,并提供一个替代它们。(PUT就 而言REST API

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

Without orphanRemovalold orders would be kept. (PATCHin terms of REST API)

没有orphanRemoval旧订单将被保留。(PATCH就 而言REST API