0

How to I load child elements and child collections using the Criteria API. I have read just about every link I could find on google, but the child elements will not get loaded.

Here is my setup

Orders have OrderItems and that is specified in my DTO's

In the fluent mapping function for the Orders, I have these notable lines:

LazyLoad();
this.HasMany<OrderItems>(x => x.OrderItems).KeyColumns.Add(new[] { "OrderId"});

In my repository class, I create the query from my criteria class and retrieve a list:

DetachedCriteria query = criteria.CreateCriteria();
IList<Orders> myOrders = 
    query.GetExecutableCriteria(UnitOfWork.CurrentSession).List<Orders>();

When I try to access the myOrders.OrderItems, they are empty (they are in my DB for sure). I assumed the LazyLoad() call in the mapping function would enable this...

So I tried to eager load like this:

query.SetFetchMode("Orders.OrderItems", NHibernate.FetchMode.Eager)

bu this doesn't work either.

Where am I going wrong?

getit
  • 623
  • 2
  • 8
  • 30

1 Answers1

0

Try this mapping for your reference:

this.References<OrderItems>(x => x.OrderItems)
    .Column("OrderId")
    .LazyLoad();

or

this.References<OrderItems>(x => x.OrderItems)
    .Column("OrderId")
    .Not.LazyLoad();
Maciej
  • 7,871
  • 1
  • 31
  • 36