The Java EE 6 Tutorial, Volume I

Orphan Removal in Relationships

When a target entity in one-to-one or one-to-many relationship is removed from the relationship, it is often desirable to cascade the remove operation to the target entity. Such target entities are considered “orphans,” and the orphanRemoval attribute can be used to specify that orphaned entities should be removed. For example, if an order has many line items, and one of the line items is removed from the order, the removed line item is considered an orphan. If orphanRemoval is set to true, the line item entity will be deleted when the line item is removed from the order.

The orphanRemoval attribute in @OneToMany and @oneToOne takes a boolean value, and is by default false.

Example 19–2 Enabling Orphan Removal in @OneToMany Relationship

The following example will cascade the remove operation to the orphaned customer entity when it is removed from the relationship.

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