How to achieve an eagerly loaded, filtered child collection with the NHibernate criteria API

Posted by vakman on Stack Overflow See other posts from Stack Overflow or by vakman
Published on 2010-05-08T07:37:43Z Indexed on 2010/05/08 7:48 UTC
Read the original article Hit count: 198

Filed under:

Is it possible to use the criteria api to load a set of parent objects along with a filtered, eagerly loaded set of child objects? I'm trying to query a list of categories and at the same time load the categories products that start with the letter M. The query below gives me the results I want but the Products are not eagerly loaded, that is NHibernate performs additional queries when I enumerate the Product collection:

 var categoriesWithProducts = session.CreateCriteria<Category>()
    .SetFetchMode("Products", FetchMode.Eager)
    .CreateCriteria("Products")
    .Add(Expression.Like("Name", "M%"))
    .List<Category>();

What am I missing here?

© Stack Overflow or respective owner

Related posts about nhibernate