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 User{
@OneToOne(orphanRemoval=true) private PhoneNum phoneNum;
}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.
PhoneNum) that should not exist without areference from an owner object (e.g.User).
- If only cascade=CascadeType.REMOVE is specified no automatic action is taken since disconnecting a relationship is not a remove
operation.