As described in
http://harald.fluffnstuff.org/wiki/2006/11/21/13.48 I'm not sure if this is a bug or a feature:
If I Load() an object with many-to-one mapped properties of classes with lazy=false these properties are loaded in the original select statement by outer joining the mapped tables of the properties classes.
Thus the Load() method needs one select statement to complete regardless how many many-to-one mappings I have specified.
If I try to do the same with HQL I instead only get Proxies for the many-to-one mapped Properties as if I wouldn't have specified lazy=false on their respective class mappings.
Explicitely adding "join fetch" clauses to the HQL statement for every many-to-one mapped Property results in the behavior i would expect in first place.
Code:
ISession session = sessionFactory.OpenSession();
IQuery query = session.CreateQuery("from DataBindingExample.Entities.Cat c where c.Id=6");
IList<Cat> list = query.List<Cat>();
Console.Out.WriteLine(list[0].Mate.Color);
session.Close();
leads to
Code:
2006-11-20 10:42:08,312 [1004] INFO NHibernate.Loader.Loader [(null)] - select cat0_.Id as Id0_, cat0_.LastModified as LastModi3_0_, cat0_.Birthdate as Birthdate0_, cat0_.Color as Color0_, cat0_.Sex as Sex0_, cat0_.Weight as Weight0_, cat0_.Created as Created0_, cat0_.Mother as Mother0_, cat0_.Mate as Mate0_, cat0_.Name as Name0_, cat0_.Discriminator as Discrimi2_ from public.Cat cat0_ where (cat0_.Id=6)
2006-11-20 10:42:08,328 [1004] INFO NHibernate.Loader.Loader [(null)] - SELECT cat0_.Id as Id0_2_, cat0_.LastModified as LastModi3_0_2_, cat0_.Birthdate as Birthdate0_2_, cat0_.Color as Color0_2_, cat0_.Sex as Sex0_2_, cat0_.Weight as Weight0_2_, cat0_.Created as Created0_2_, cat0_.Mother as Mother0_2_, cat0_.Mate as Mate0_2_, cat0_.Name as Name0_2_, cat0_.Discriminator as Discrimi2_2_, cat1_.Id as Id0_0_, cat1_.LastModified as LastModi3_0_0_, cat1_.Birthdate as Birthdate0_0_, cat1_.Color as Color0_0_, cat1_.Sex as Sex0_0_, cat1_.Weight as Weight0_0_, cat1_.Created as Created0_0_, cat1_.Mother as Mother0_0_, cat1_.Mate as Mate0_0_, cat1_.Name as Name0_0_, cat1_.Discriminator as Discrimi2_0_, cat2_.Id as Id0_1_, cat2_.LastModified as LastModi3_0_1_, cat2_.Birthdate as Birthdate0_1_, cat2_.Color as Color0_1_, cat2_.Sex as Sex0_1_, cat2_.Weight as Weight0_1_, cat2_.Created as Created0_1_, cat2_.Mother as Mother0_1_, cat2_.Mate as Mate0_1_, cat2_.Name as Name0_1_, cat2_.Discriminator as Discrimi2_1_ FROM public.Cat cat0_ left outer join public.Cat cat1_ on cat0_.Mother=cat1_.Id left outer join public.Cat cat2_ on cat1_.Mate=cat2_.Id WHERE cat0_.Id=:p0
while
Code:
ISession session = sessionFactory.OpenSession();
IQuery query = session.CreateQuery("from DataBindingExample.Entities.Cat c join fetch c.Mate join fetch c.Mother where c.Id=6");
IList<Cat> list = query.List<Cat>();
Console.Out.WriteLine(list[0].Mate.Color);
session.Close();
leads to the originally expected
Code:
2006-11-20 10:45:38,656 [5736] INFO NHibernate.Loader.Loader [(null)] - select cat0_.Id as Id0_0_, cat1_.Id as Id0_1_, cat2_.Id as Id0_2_, cat0_.LastModified as LastModi3_0_0_, cat0_.Birthdate as Birthdate0_0_, cat0_.Color as Color0_0_, cat0_.Sex as Sex0_0_, cat0_.Weight as Weight0_0_, cat0_.Created as Created0_0_, cat0_.Mother as Mother0_0_, cat0_.Mate as Mate0_0_, cat0_.Name as Name0_0_, cat0_.Discriminator as Discrimi2_0_, cat1_.LastModified as LastModi3_0_1_, cat1_.Birthdate as Birthdate0_1_, cat1_.Color as Color0_1_, cat1_.Sex as Sex0_1_, cat1_.Weight as Weight0_1_, cat1_.Created as Created0_1_, cat1_.Mother as Mother0_1_, cat1_.Mate as Mate0_1_, cat1_.Name as Name0_1_, cat1_.Discriminator as Discrimi2_1_, cat2_.LastModified as LastModi3_0_2_, cat2_.Birthdate as Birthdate0_2_, cat2_.Color as Color0_2_, cat2_.Sex as Sex0_2_, cat2_.Weight as Weight0_2_, cat2_.Created as Created0_2_, cat2_.Mother as Mother0_2_, cat2_.Mate as Mate0_2_, cat2_.Name as Name0_2_, cat2_.Discriminator as Discrimi2_2_ from public.Cat cat0_ inner join public.Cat cat1_ on cat0_.Mate=cat1_.Id inner join public.Cat cat2_ on cat0_.Mother=cat2_.Id where (cat0_.Id=6)