views:

49

answers:

1

A simple question regarding how NHibernate works:
I have a parent entity that has a collection of other child entities. Those child entities have a reference to the parent entity they belong to. Now I want to implement an Add method to the parent entity that would add a child to it. Should that Add method only add the child to its new parents collection, or should it also update the parent reference of the child or should it also remove the added entity from its previous parents collection? Do I have to do all these things in that method or will NHibernate do something for me?

Thanks.

A: 

You should definitely make sure that the relationship is "valid" for all parties involved.

Make sure that the parent has the child in its children collection, and the the child has the parent in its parent reference, and that the child is removed from its old parent (if it had one). The last step is the only one for which I'm not sure it's mandatory.

Sapph
That's the problem. Most of the examples on the internet have first two things, but it is never explicitly stated whether the last step is omitted for simplicity of because it is in fact not needed and handled by NHibernate. Since NHibernate keeps track of the objects during a session, it theoretically could track a single change and do the other two steps automatically, but again, I couldn't find any information like that either, so that's why I am asking. (And well, also because I am lazy to write a couple of simple tests...)
karel_evzen