You would expect that this code would load pages.Culture[localCulture] every time it executes. Well NHibernate does not think so and instead loads pages.Culture[localCulture] only ONCE in session. So due to lazy loading everything else in pages.Culture will NEVER get loaded as trying to access something like pages.Culture[2] will just throw an exception of trying to access an object which is null. Yeah you could try loading pages.Culture[2].Whatever to try to force NHibernate load it from database, but if it doesn't exist you just end up with another exception. Yet if the join returns 0 results NHibernate will not load the object at all (at is should), but if there is 1 result (or more) it will just fetch you the query it executed earlier.
I don't see any logic in such behavior. If I explicitly execute a query to load that exact part of collection it should be loaded, not just checked for existance (as of what it does on second execution of the same query).
IList pages = DBSession.CreateQuery("select distinct from DBMap.Pages t1 " +
"join fetch t1.Locales t2 " +
"where t1.System = 0 and t2.Culture = " + localCulture +
" order by Title")
.List();
|