Take a look on this class:
http://unhaddins.googlecode.com/svn/bra ... Fixture.cs
This code:
Code:
using (new SqlLogSpy())
using (ISession s = OpenSession())
{
IList<Cat> catsFound = s.CreateQuery("from Cat c where size(c.Kittens) > 0").List<Cat>();
foreach (Cat cat in catsFound)
{
log.Debug(">>> Cat: " + cat.Name);
// Tell NH we want the kittens, so hitting the db is needed.
foreach (Cat kitten in cat.Kittens)
{
log.Debug(">>> Kitten: " + kitten.Name);
}
}
}
Reproduce this output:
Code:
2008-02-13 22:38:53,718 DEBUG SQL:386 - select cat0_.Id as Id0_, cat0_.Name as Name0_ from Cat cat0_ where ((select count(*) from Cat kittens1_ where cat0_.Id=kittens1_.ParentId)>0 )
2008-02-13 22:38:59,906 DEBUG TestCase:57 - >>> Cat: Parent number 0
2008-02-13 22:39:16,421 DEBUG SQL:386 - SELECT kittens0_.ParentId as ParentId__1_, kittens0_.Id as Id1_, kittens0_.Id as Id0_0_, kittens0_.Name as Name0_0_ FROM Cat kittens0_ WHERE kittens0_.ParentId in (@p0, @p1, @p2); @p0 = '1', @p1 = '3', @p2 = '5'
2008-02-13 22:39:18,796 DEBUG TestCase:62 - >>> Kitten: Kitten of cat 0
2008-02-13 22:39:24,843 DEBUG SQL:386 - SELECT kittens0_.ParentId as ParentId__1_, kittens0_.Id as Id1_, kittens0_.Id as Id0_0_, kittens0_.Name as Name0_0_ FROM Cat kittens0_ WHERE kittens0_.ParentId in (@p0, @p1, @p2); @p0 = '2', @p1 = '4', @p2 = '6'
2008-02-13 22:39:28,171 DEBUG TestCase:57 - >>> Cat: Parent number 1
2008-02-13 22:39:28,171 DEBUG TestCase:62 - >>> Kitten: Kitten of cat 1
2008-02-13 22:39:28,765 DEBUG TestCase:57 - >>> Cat: Parent number 2
2008-02-13 22:39:28,765 DEBUG TestCase:62 - >>> Kitten: Kitten of cat 2
2008-02-13 22:39:29,234 DEBUG TestCase:57 - >>> Cat: Parent number 3
2008-02-13 22:39:29,234 DEBUG SQL:386 - SELECT kittens0_.ParentId as ParentId__1_, kittens0_.Id as Id1_, kittens0_.Id as Id0_0_, kittens0_.Name as Name0_0_ FROM Cat kittens0_ WHERE kittens0_.ParentId in (@p0, @p1, @p2); @p0 = '7', @p1 = '9', @p2 = '11'
2008-02-13 22:39:29,250 DEBUG TestCase:62 - >>> Kitten: Kitten of cat 3
2008-02-13 22:39:30,531 DEBUG TestCase:57 - >>> Cat: Parent number 4
2008-02-13 22:39:30,546 DEBUG TestCase:62 - >>> Kitten: Kitten of cat 4
2008-02-13 22:39:30,546 DEBUG TestCase:57 - >>> Cat: Parent number 5
2008-02-13 22:39:30,546 DEBUG TestCase:62 - >>> Kitten: Kitten of cat 5
2008-02-13 22:39:30,546 DEBUG TestCase:57 - >>> Cat: Parent number 6
2008-02-13 22:39:30,546 DEBUG SQL:386 - SELECT kittens0_.ParentId as ParentId__1_, kittens0_.Id as Id1_, kittens0_.Id as Id0_0_, kittens0_.Name as Name0_0_ FROM Cat kittens0_ WHERE kittens0_.ParentId in (@p0, @p1, @p2); @p0 = '13', @p1 = '8', @p2 = '10'
2008-02-13 22:39:30,562 DEBUG TestCase:62 - >>> Kitten: Kitten of cat 6
2008-02-13 22:39:30,750 DEBUG TestCase:98 - TearDown
As you can see... the parent go for the kittens at iteration time.
The class and mapping you can find here:
http://unhaddins.googlecode.com/svn/bra ... /LazyLoad/