This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
Beware using filters and where clause restrictions to filter your NHibernate collections fetched with join. There is a known problem (JIRA issue NH-1930) which can be described like this:
- You have a collection (set, bag, list, etc) mapped with the join fetch mode (eagerly loaded):
- You apply to it a where clause or a filter;
- The restriction is applied to the generated SQL’s global where clause:
instead of the INNER JOIN ON clause:
- If the related table does not have rows that match the association, no records will be returned at all, not even for the main entity that you are querying!
In order to go around it, you can either turn to using select as the fetch mode, disable the restriction or specify it like "DELETED = 0 OR DELETED = NULL". Or, of course, wait for the issue to be fixed!