SteelReed

Search

Search posts, questions…

JPA & HibernateQuestion 10 / 24

Middle

How do CascadeType.REMOVE and orphanRemoval differ?

Short answer

CascadeType.REMOVE deletes children when the parent is deleted. orphanRemoval = true deletes a child when it is detached from the parent, even though the parent stays.

delete orderitems.remove(i)yesnoOrder with itemsboth: delete itemsorphanRemoval?DELETE itemonly unlink

3 points to say:

  1. Use orphanRemoval only when a child belongs to one parent.
  2. With orphanRemoval, clear() + addAll(); never assign a new list.
  3. Cascade is Hibernate behaviour, not a database constraint.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.