2

In Criteria I do SetFetchMode as Lazy but still fetching all items, how can I fix this?

public class MenuItem : BaseClass<MenuItem>
{
    public virtual int MenuItemId { get; set; }
    public virtual string Text { get; set; }
    public virtual IList<MenuItem> Children { get; set; }
    public virtual MenuItem Parent { get; set; }

    public MenuItem()
    {
         Children = new List<MenuItem>();
    }
}

class MenuItemMap : ClassMap<MenuItem>
{
    public MenuItemMap()
    {
        Id(x => x.MenuItemId);
        Map(x => x.Text);
        HasMany(x => x.Children).KeyColumn("ParentId").Not.LazyLoad().Fetch.Select();
        References(x => x.Parent).Not.LazyLoad().Fetch.Select(); 
    }
}

using (var session = NHibernateHelper<T>.OpenSession())
{
    var CC = session.CreateCriteria(typeof(T));

    CC.SetFetchMode("Children", FetchMode.Lazy);

    return CC.List<T>();
}
3
  • Can you show us the class definition of MenuItem? Especially how you defined the Children collection Commented May 11, 2015 at 12:04
  • I added class definition Commented May 11, 2015 at 12:05
  • I had hoped you had forgotten to use virtual everywhere, or had used List<MenuItem> Children :-( Commented May 11, 2015 at 12:06

1 Answer 1

2

I have to say, that this is not possible. Our JAVA brother Hibernate (from which NHibernate was ported into .NET) seems to have that option:

But NHibernate does not support that. Chek also Override lazy loading behavior 'lazy=false'.

What we can do, is to bet on Projections. This way we really assemble exactly one SQL statement and get a list of Transformed objects (semi-filled, dependent on amount of selected properties)

16.6. Projections

Here is an example how to build a deep projections (including References/many-to-one)
Here is how to transform them into originally mapped object Graph.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.