4

I'm trying to eager load roles in many to many collection off of my User object.

Role role = null;
IQueryOver<User, User> query = session.QueryOver<User>()
                                .Fetch( p => p.Roles).Eager
                                .JoinAlias( q => q.Roles, () => role)
                                .Where(() => role.Active == true);

leaves me with user objects that have uninitialized roles members. If I remove the joinalias, they are initialized just fine. Is this just an NH3 bug or am I doing something wrong?

Darius Kucinskas
  • 10,193
  • 12
  • 57
  • 79
gt124
  • 1,238
  • 13
  • 23

2 Answers2

13

Another way to make eager load is to set LeftOuterJoin. It helped to us in a similar scenario

Role role = null;
IQueryOver<User, User> query = session.QueryOver<User>().Fetch( p => p.Roles).Eager
                                                        .JoinAlias( q => q.Roles, () => role, JoinType.LeftOuterJoin)
                                                        .Where(() => role.Active == true);
Community
  • 1
  • 1
Sly
  • 15,046
  • 12
  • 60
  • 89
  • I don't think you actually need the `.Fetch( p => p.Roles).Eager` here. The left join apparently does the eager loading. http://puredotnetcoder.blogspot.ie/2011/09/queryover-eager-loading-parent-and.html – Cole W Jan 21 '16 at 14:10
1

That's the expected behavior. If you use JoinAlias, you'll be filtering the collection elements, so it can't be initialized.

You need to use a subquery for filtering if you intend to use eager loading. on the same collection.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154