Reputation: 3619
I wanted to clarify an assumption that I have made, regarding the functionality of Hibernate
. Assume I have a Class of A
's with a @OneToMany
mapping with B
's. These B's each have an A
parent reference to support a mappedBy
attribute on A.
When I remove a B
from the collection in A
, does hibernate have the ability to automatically null out the parent field inside of B
?. From all of the tests I have done, when removing something from a collection, it doesn't actually update the database by changing the parent reference in the child.
This link seems to support my claim as they manually null out the parent reference, along with removing it from the parent Set
.
Upvotes: 4
Views: 4447
Reputation: 70564
Whenever an association is mapped from both ends, one of these ends is designated the active, and one the passive end. (The passive end is the one mapped using mappedBy=
or inverse="true"
)
Hibernate does not, and can not, update the passive end of a collection when the active end is changed, as it can detect such changes only at flush time. Hence it is considered good practice for calling code to modify both ends of the association, to ensure the object model is always in a consistent state.
Hibernate itself does not care whether the two ends of the association are consistent, as it only looks at the active end when flushing to the database.
When mapping a one-to-many association, the one-to-many end should be designated passive. The section from the hibernate manual you link to attempts to explain why.
Upvotes: 7