简体   繁体   中英

nhibernate queryover not loading eagerly with a many to many joinalias

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?

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM