R – NHibernate – problem with eager loading

hqlnhibernate

I have a ClassA that has many ClassB elements:

public abstract class ClassA: IEntity<ClassA>
{
    public virtual IList<ClassB> ClassBList { get; protected set; }
     ...
}

Then ClassB references ClassC element:

public class ClassB 
{

    public  ClassC CEntity { get; private set; }

    public  Percentage Percentage{ get; private set; }
    ...
}

and here is the mapping for ClassA (ClassB has no mapping):

        HasMany(a => a.ClassBList).Component(a =>
        {
            a.References(b => b.CEntity , "IDCEntity").ForeignKey("ID").Cascade.SaveUpdate().Not.LazyLoad();
            t.Map(b => b.Percentage, "Percentage");
            ...;
        }).AsBag().Cascade.AllDeleteOrphan().Not.LazyLoad()

When i get all classA elements from the database it brings all the ClassB from ClassBList loaded, but it doesn't load ClassC elements in ClassB.

i tried the following HQL:

                var query = session.CreateQuery("select a from ClassA as a left join fetch  a.ClassBList as b left join fetch b.CEntity as c");

            classAList = query.List<ClassA>();

but i get this error:

Exception = {"Query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=c,role=,tableName=ClassCTable,tableAlias=classc2_,or…

i think this happens because i'm trying to eager load ClassC element of classB, but classB is not in hte select list.

What is the alternative, or the correct HQL?

Note: i cannot use criteria because of this problem:

Best Answer

If the problem is to eager load through a complex association, that spans 3 entities, maybe this blog post will help

Related Topic