Question: How to get an impatient loaded criterion for returning paged results in the root entity with all sets of child sets fetchmode = eager.
I am trying to get a result set with a set of 10 elements with child collections loaded. The problem is that the top 10 in the query is wrapped around the entire selection. Reasons only return the first 10 results, including all merged records. If the first object has 10 children, then my result set will return 1 object with 10 children loaded. I need entities, and children's collections come back hydrated (lazy). If I refuse lazy loading and running this query, I get a query n + 1 for each associated element in the result set.
This is my main request process:
criteria = context.Session.CreateCriteria<Associate>();
criteria.SetMaxResults(10);
criteria.SetFirstResult(1);
criteria.SetFetchMode("Roles", NHibernate.FetchMode.Eager);
criteria.SetFetchMode("Messages", NHibernate.FetchMode.Eager);
criteria.SetFetchMode("DirectReports", NHibernate.FetchMode.Eager);
criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
return criteria.List<Associate>();
public AssociateMap()
{
ReadOnly();
Id(x => x.AssociateId);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.ManagerId);
Map(x => x.Department);
Map(x => x.Email);
Map(x => x.JobTitle);
Map(x => x.LastFirstName).Formula("LTRIM(RTRIM(LastName)) + ', ' + LTRIM(RTRIM(FirstName))");
HasMany(x => x.Messages).KeyColumn("AssociateId").Inverse().Cascade.All();
HasMany(x => x.Roles).Element("RoleKey");
HasMany(x => x.DirectReports).KeyColumn("ManagerId").Cascade.None().ForeignKeyConstraintName("FK_Associate_Manager");
}
source
share