1. The dataset returned is small
2. We don't care about the cartesain product.
3. We want to load the parents
4. We want to load the children
5. We want to filter the results based on a where clause on the children
So how do we go about this, the domain model for this example looks like this:-
public class Movie : Entity { public virtual string Director { get; set; } public virtual IList<Actor> ActorList { get; set; } } public class Actor : Entity { public virtual string Name { get; set; } public virtual string Role { get; set; } }When using NHibernate and QueryOver we would specify eager loading by using:-
Actor actor = null; return Session.QueryOver<Movie>() .Fetch(f => f.ActorList).Eager .Left.JoinAlias(f => f.ActorList, () => actor) .Where(() => actor.Name.Like("m%")) .List();Lets look at the SQL output that gets generated:-
Excellent this is exactly what I require, one sql statement is sent to the database and both parents and children are hydrated at the same time.
However, there is one caveat here, look closely at my c# code and you will see that I am specifying a left join.
Now for me this is where the some confusion crops up. If I take out the .left out of my code so that it is:-
return Session.QueryOver<Movie>() .Fetch(f => f.ActorList).Eager .JoinAlias(f => f.ActorList, () => actor) .Where(() => actor.Name.Like("m%")) .List();and rerun my example the sql that gets generated is:-
As you can see we have resorted back to lazy loading, this is quite strange and I am not 100% sure if this is a bug or the intended behaviour.
So it looks like if you want to take advantage of eager loading and queryover then you always need to specify a .left join in your code!
Very good, had the same problem because the LEFT was not working
ReplyDeleteThanks. You saved my day :)
ReplyDeleteStill a useful article in 2016, thank you!
ReplyDeleteJust came across this problem, and I agree that this article is still relevant. Solved the issue I was having
ReplyDeleteGreat. Still an issue in 2017.
ReplyDeleteThanks. You saved my day in 2020 :)
ReplyDeleteAnkara
ReplyDeleteBolu
Sakarya
Mersin
Malatya
V3HS
görüntülü.show
ReplyDeletewhatsapp ücretli show
URZM
13F79
ReplyDeleteKripto Para Nedir
Ünye Organizasyon
İzmir Şehirler Arası Nakliyat
Tokat Evden Eve Nakliyat
Bartın Şehirler Arası Nakliyat
Lovely Coin Hangi Borsada
Karaman Parça Eşya Taşıma
Siirt Şehir İçi Nakliyat
Osmo Coin Hangi Borsada