Hibernate - remove item from collection

I wanted to clarify the assumption I made regarding the functionality of Hibernate . Suppose I have class A with @OneToMany mapping with B These B have a parent link A to support the mappedBy attribute on A.

When I delete B from a collection in A , hibernate has the ability to automatically reset the parent field inside B ?. Of all the tests I did, when I delete something from the collection, it actually does not update the database by changing the parent link in the child element.

This link seems to support my requirement, as they manually exclude the parent link and remove it from the parent Set .

+4
source share
1 answer

Whenever an association is mapped at both ends, one of these ends is designated as active and the other as passive. (The passive end is the one that is displayed using mappedBy= or inverse="true" )

Hibernation does not update the passive end of the collection when updating the active end and cannot update it, because it can detect such changes only during a reset. Therefore, it is considered good practice to call code to change both ends of the association to ensure that the object model is always in a consistent state.

Hibernation alone does not care about whether the two ends of the association are consistent, since it only looks at the active end when flushing the database.

When comparing a one-to-many association, the one-to-many end should be marked as passive. The section from the sleep guide you are referencing is trying to explain why.

+6
source

All Articles