1

For NHibernate 3.3.3

Think about three entity like

public class Menu{
    public string Name{get;set;}
    public IList<MenuAccess> MenuAccessList{get;set}
}

public class MenuAccess{
   public string Name{get;set;}
   public Controller MenuController{get;set;}
}

public class Controller{
    public long Id{get;set;}
    public string Name{get;set;}
}

consider their lazy load enabled mapping

now if in criteria query we use SetFetchMode(FetchMode.Eager) on MenuAccessList that works fine but if we create alias of MenuAccessList and add condition on that then Eagerload do not work. Throw lazy load exception if we access that later.

using(ISession session = NHSessionFactory.OpenSession()){
    ICriteria criteria = session.CreateCriteria<Menu>();                
    criteria.CreateAlias("MenuAccessList", "ma", JoinType.InnerJoin);
    criteria.SetFetchMode("ma", FetchMode.Eager);
    criteria.Add(Restrictions.Eq("ma.MenuController.Id",10L));
    var list = criteria.List<Menu>();
}

This query cannot load MenuAccessList from menu if we access that outside session scope.

Abdur Rahman
  • 353
  • 5
  • 16
  • *Before you will go this way, I would suggest to re-evaluate the fetch strategy for collection. [Here](http://stackoverflow.com/a/28359510/1679310) or [there](http://stackoverflow.com/a/20970816/1679310) are some arguments and links ... just another view...* – Radim Köhler Mar 15 '15 at 13:49

1 Answers1

0

Try using the full name instead of the alias:

criteria.SetFetchMode("MenuAccessList", FetchMode.Eager);
xanatos
  • 109,618
  • 12
  • 197
  • 280