I’ve read many posts about this, the last being a statement saying the behaviour i’m experiencing the the expected if not preferred way.
I’ve got a handful of domain models mapped to their database tables.
I’m using a Criteria object to query them.
var query = session.CreateCriteria(typeof(Posting))
.CreateAlias("Person", "person", JoinType.InnerJoin).SetFetchMode("Person", FetchMode.Eager)
.CreateAlias("Location", "location", JoinType.InnerJoin).SetFetchMode("Location", FetchMode.Eager)
.CreateAlias("Post", "post", JoinType.InnerJoin).SetFetchMode("Post", FetchMode.Eager)
.CreateAlias("post.Zone", "postzone", JoinType.LeftOuterJoin).SetFetchMode("post.Zone", FetchMode.Select)
It produces the exact statment I’m looking for. I’d expect it to populate the objects eagerly (which it does) and leave nulls for post.Zone where the relationship fails.
In my mapping i’ve had to say not-found=ignore (Notfound.Ignore() FNH) which due to the outer join is causing NH to generate a number of sub-query – all on the Zone table – which doesn’t seem necessary to me, especially as it already had the data it need to populate the Zone object.
Can anyone she any light on any changes I can make to the Mapping or Query/Criteria to keep this load to a single query, or should I expect an extra statement for every record outer joined to?
Thanks,
Sam
It is unfortunatly a known issue that
not-found=ignorecauses additional selects. There have been bug reports filed for this in both NHibernate and Hibernate for quite some time (2007) and currently no known work around has been made available other than removingnot-found=ignoreor changing the data present in the database so it is no longer required.A possible work around, although not a nice one, would be to have a stored procedure perform the query you want and then call this from NHibernate. This would remove the redundant queries.