Ok, I thought I had this sorted, but it appears not, and I thought it was simple, so I'm finding it a bit frustrating wiring up these entities.
I've implemented the approach discussed by jnapier in this thread here:
http://forum.hibernate.org/viewtopic.ph ... c&start=15
to use a custom list implementation with events on the Add and Remove methods to ensure that parent relationships are set up when the child is added to the collection. I'm still not entirely clear on how to manage the relationship when the child is added to the parent.
i.e. the scenario:
Code:
parent.Children.Add(child)
works fine, because the event that gets triggered on the add has the code for child.Parent = this;
However, when I come from the other direction, if I do:
Code:
child.Parent = p;
and then in the setter for child.Parent do something along the lines of:
Code:
set
{
this.Parent = value;
if(!value.Children.Contains(this))
{
value.Children.Add(this);
}
}
I get a LazyInitializationException : cannot access loading collection.
What's the (or a) right way to manage a bidirectional parent/child relationship that will be used from both directions?