Why do I get null objects in a many-to-many bag?
- by Jim Geurts
I have a bag defined for a many-to-many list:
<class name="Author" table="Authors">
<id name="Id" column="AuthorId">
<generator class="identity" />
</id>
<property name="Name" />
<bag name="Books" table="Author_Book_Map" where="IsDeleted=0" fetch="join">
<key column="AuthorId" />
<many-to-many class="Book" column="BookId" where="IsDeleted=0" />
</bag>
</class>
If I return all author objects using something like the following, I will get what initially appeared to be duplicate Author records:
Session.Query<Author>().List<Author>()
The extra author objects are created when an author is mapped to Book objects that have IsDeleted = 1 and IsDeleted = 0. Rather than creating one Author object with an enumerable that contains only the books with IsDeleted = 0, it will create two author objects. The first author object has a Books enumerable that contains books with IsDeleted = 0. The second author object will contain an enumerable of null book objects.
Similarly, if an object only has one book map, and that map points to a book with IsDeleted = 1, then an author object is returned with a Books collection having one null object.
I'm thinking part of the problem stems from the map table objects linking to rows that satisfy the where condition on the bag object but do not meet the many-to-many where condition.
This is happening with NHibernate version 3.0.0.4980. Is this a configuration issue or something else?