Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nhibernate queryover not loading eagerly with a many to many joinalias

Tags:

nhibernate

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?

like image 671
gt124 Avatar asked Dec 09 '22 13:12

gt124


2 Answers

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);
like image 74
Sly Avatar answered Dec 12 '22 02:12

Sly


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.

like image 43
Diego Mijelshon Avatar answered Dec 12 '22 02:12

Diego Mijelshon