An example taken form here:
When an Employee
entity object is removed, the remove operation is cascaded to the referenced Address
entity object. In this regard, orphanRemoval=true
and cascade=CascadeType.REMOVE
are identical, and if orphanRemoval=true
is specified, CascadeType.REMOVE
is redundant.
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=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.
To avoid dangling references as a result of orphan removal, this feature should only be enabled for fields that hold private non shared dependent objects.
I hope this makes it more clear.