I have 2 classes in a many-to-many relationship, each with an ICollection of the other:
public class Person {
public ICollection<Building> Buildings { get; private set; }
}
public class Building {
public ICollection<Person> People { get; private set; }
}
In the mapping file, they're declared as many-to-many:
<class name="Person" table="people">
<set name="Buildings" table="personbuilding">
<key column="personId">
<many-to-many class="Building" column="buildingId"/>
</set>
</class>
<class name="Building" table="buildings">
<set name="People" table="personbuilding">
<key column="buildingId">
<many-to-many class="Person" column="personId"/>
</set>
</class>
In most places where I call session.Update(person), it works fine: it updates the record, and preserves the Buildings list. In one place, however, session.Update(person) removes all records from the 'personbuilding' table with this personId -- not cool.
It's some combination of "session.Get(id) doesn't always load the Buildings list" and "session.Update(person) saves the Buildings list even though I don't want it to".
A simple workaround would be to fix the latter, i.e., make it stop trying to update the personbuilding table (I do that myself, outside of NHibernate -- I use NHibernate on that table only for queries), but I don't see how to do that. After reading the docs, I'm still not sure if cascade="none" is supposed to do that or not (does it hold off on saving links, too, or just linked objects?), but it doesn't seem to.
I also tried lazy="false", to try to get it to load the Buildings list on a Person all the time, but I couldn't make that do anything, either.
I'm really not clear on when it loads/saves the ICollections for a many-to-many relationship. Can anybody help me out here?