Removing a child from an entity collection does not necessarily delete the child’s database row. On a @OneToMany association, orphanRemoval = true tells JPA to delete a child permanently when it is removed from that collection. section 8.3.3
The mapping enables orphan removal alongside CascadeType.PERSIST. The two settings address different transitions: persisting referenced children and deleting children removed from the collection. section 8.3.3
Remove one child
Fetch an Item with its bids, call item.removeBid(firstBid), and save the Item. Hibernate detects the removed Bid at transaction commit and issues a SQL DELETE for it. section 8.3.3
Choose the deletion mechanism according to the relationship:
- Orphan removal deletes a child removed from the collection; CascadeType.REMOVE instead cascades deletion when the parent is deleted. section 8.3.3
- Unlike clearing a collection of embeddable components, clearing entity children does not produce one bulk DELETE: Hibernate loads and removes them individually. section 8.3.3
- Enable orphan removal only when no other database row or in-memory association needs the child. Deleting its row does not clean up other in-memory references. section 8.3.3
