Thursday, October 30, 2014

JPA orphanRemoval=true VS CascadeType.REMOVE

A short comparison of  orphanRemoval and CascadeType.REMOVE properties.

CascadeType.REMOVE

CascadeType.REMOVE (and also CascadeType.ALL as a private case) tells the DB to delete all child records when the parent is deleted. That is if I delete the INVOICE, then delete all of the ITEMS on that INVOICE.

orphanRemoval=true

orphanRemoval=true tells the ORM that if I remove an Item object from the collection of Items that belong to an Invoice object (in memory operation), and then "save" the Invoice, the removed Item should be deleted from the underlying DB. In private case, the "collection" could be @OneToOne relationship, not just @OneToMany.


Source: some (not top rated ones!) of the comments in this stackoverflow thread.

No comments:

Post a Comment