tags:

views:

75

answers:

1

Is it possible to do eager loading with HQL without touching mappings? The "left join fetch" expression is completely ignored by nHibernate.

var query = Session.CreateQuery("from o in Member left join fetch o.Photos");
query.List<Member>();

The generated SQL is

SELECT this_.Id as Id7_1_, this_.Name as Name7_1_, this_.AltNames as AltNames7_1_,
this_.Tags as Tags7_1_, this_.Loved as Loved7_1_, profile2_.id as id6_0_,
profile2_.Website as Website6_0_, profile2_.Email as Email6_0_, 
profile2_.Shop as Shop6_0_ 
FROM Member this_ 
left outer join member_profile profile2_ on this_.Id=profile2_.id 
limit 10;

And then 10 statements grabbing the photos. MemberProfile is mapped as OneToOne.

+2  A: 

You could use the fetch keyword:

from Cat as cat inner join fetch cat.Mate

This will eagerly load the Mate association.

Darin Dimitrov
It just completely ignores the "join fetch" part.
HeavyWave
Could you provide more context and show your code?
Darin Dimitrov
How about a left join: `from Cat as cat left join cat.Kittens`?
Darin Dimitrov
I've updated the question. Any type of join fetch is ignored.
HeavyWave
You HQL syntax is strange. Try this: `from Member as m left join fetch m.Photos`
Darin Dimitrov
I've figured it out, there was some other bug in my code. Still thanks for the answer, good to know.
HeavyWave
I've sorted my code. Now nHibernate ignores SetMaxResults if I use your query and grabs everything from the database.
HeavyWave