1 Answer
1

If it is a many-to-many, then one direction is the "master" direction. The other direction will have inverse="false" defined. You have to remove from the collection the right way round - to be sure (and to truly represent what your deletion is achieving), delete both. Then flush the session to have this persisted.

Example:

if A has a collection of objects of type B, and vice versa, then to remove the association between two particular instances, you have to delete the instance of A from the instance of B's collection and vice versa. Then when the session is flushed, NHibernate knows to delete the relevant row from the linking table.

To tidy up your code to delete a team, which is not responsible for the relationship, you do have to iterate over the linked employees. However, if you have to delete teams more frequently than employees, then you may find it more convenient to reverse this, and then you can just use:

what is the right way to remove from the collection? I used my calls in the NH transaction scope, so afaik I dont need to flush manually.
–
RookianJan 2 '10 at 20:27

Can you edit your question to include the relevant bits of class and mapping definitions?
–
David MJan 2 '10 at 20:31

there it is. I wanna delete a team (child) from the employee (parent).
–
RookianJan 2 '10 at 20:38

ah cool yes this works! But how can I write the code better? Look at the edited question...
–
RookianJan 2 '10 at 21:28

team.Employees.Clear(); looks pretty ugly in my opinion, but it should work because NHibernate would only really delete the team. But when I reuse the Employees collection it is empty and I think this is wrong.
–
RookianJan 2 '10 at 22:02