NHibernate: can't successfully eager load
Asked Answered
F

1

8

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?

Fake answered 7/12, 2010 at 18:39 Comment(2)
hmmm...I thought 1-to-1 were always eager fetched...in fact I remember NOT being able to lazy load a 1-to-1. Did this change with NH3?Derickderide
My reference is NH Profiler. I see 2 requests, one select for User and one select for Profile. I must admit I didn't look at that when I was using NH2.Fake
T
3

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.

Towardly answered 9/12, 2010 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.