Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate: can't successfully eager load

I'm using NH 3.0 and FNH 1.1 recompiled with NH3.

I have a user model where I want to always retrieve its profile when loading it. I use the linq provider from NH3 but can't use its Fetch method (because of my repository that hides NHibernate and returns an IQueryable and the fact that ToPagedList is called on the query therefore preventing me to put Fetch as the last call of the query).

In the UserMap I set:

HasOne(x => x.Profile)
    .Not.LazyLoad()
    .Cascade.All();

But setting LazyLoad to OFF does not help. I played with the fetch mode too.

My expectation is that if I define this mapping, then I should not even have to tell Linq that I want Profile to be fetched when the User entity is requested. Linq should honour the mapping, no?

like image 759
Nicolas Cadilhac Avatar asked Dec 07 '10 18:12

Nicolas Cadilhac


1 Answers

I am having this problem as well, and unfortunately I think it is by design. The NHibernate 3.0 Linq provider uses HQL under the covers, and HQL doesn't honor your mappings in this respect. For example, if you did

session.CreateQuery("from Profile").List()

You would only get a list of all profiles and your user class would not join even if your mapping has outer-join=true.

If you were using the old NHibernate.Linq provider that used the Critera API, or the Criteria API directly:

session.CreateCriteria().List()

you would get back a list of all profiles left outer joined with users, just as your mapping file requested.

Now I don't know why the HQL backed linq provider doesn't honor your mappings (and if anyone knows a way around this, please post), but I believe that is why you are seeing this behavior.

like image 116
Scott Kirkland Avatar answered Oct 23 '22 13:10

Scott Kirkland