How to eager fetch multi level objects with NHibernate

424 views Asked by At

I have the following Entity's:

public class Parent
{
    public string Id { get; set; }

    public Ilist<Child> Children { get; set; }
}

public class Child
{
    public string Id { get; set; }

    public Ilist<Parent> Parents { get; set; }

    public Ilist<Grandchild> GrandChildren { get; set; }
}

public class Grandchild
{
    public string Id { get; set; }

    public Child parent { get; set; }
}

And their mappings:

public class ParentMap
{
    HasManyToMany(x => x.Children).Table("P_C").LazyLoad();
}

public class ChildMap
{
    HasManyToMany(x => x.Parent).Table("P_C").Inverse();

    HasMany(x => x.Parents).KeyColumn("ParentID").Cascade.All().Inverse();
}

public class GrandchildMap
{
    References(x => x.parent).Column("ParentID").Not.Nullable();
}

I want to fetch Eager all the levels using NHibernate 4 When i try Getting a single Parent by ID like:

var Parent = session.QueryOver<Parent>().Where(x => x.Id == Id).
                             Fetch(x => x.Children).Eager.
                             Fetch(x => x.Children.First().GrandChildren).Eager.
                             TransformUsing(Transformers.DistinctRootEntity).
                             List<Parent>().FirstOrDefault();

I get "cannot simultaneously fetch multiple bags" Exception

0

There are 0 answers