I have an application that loads objects via hibernate and then passes those objects to another tier as detached objects. Any changes to those objects are sent back down to the hibernate tier where I call saveOrUpdate()
on those objects.
Will hibernate delete one-to-many relationship child objects contained in a collection in the objects that are passed into saveOrUpdate()
if I simply remove the child object from the collection before calling saveOrUpdate()
?
If not, then how would this typically be accomplished in a hibernate application that uses detached objects?
Will hibernate delete one-to-many relationship child objects contained in a collection in the objects that are passed into
saveOrUpdate()
if I simply remove the child object from the collection before callingsaveOrUpdate()
?
No, not by default. Such child objects are known as "orphans" in this context, assuming that some other entity doesn't also have a reference to them.
This is discussed in the docs, 11.11. Transitive persistence:
A special cascade style,
delete-orphan
, applies only to one-to-many associations, and indicates that thedelete()
operation should be applied to any child object that is removed from the association. Using annotations there is noCascadeType.DELETE-ORPHAN
equivalent. Instead you can use the attribute orphanRemoval as seen in Example 11.4, “@OneToMany
withorphanRemoval
”. If an entity is removed from a@OneToMany
collection or an associated entity is dereferenced from a@OneToOne
association, this associated entity can be marked for deletion iforphanRemoval
is set to true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With