NHibernate paging criteria with fetchmode eager. (using fluent NH) -


question: how eager loaded criteria return paged results on root entity child collections set fetchmode = eager.

i trying 10 item paged result set eager loaded child collections. problem query select top 10 wrapped around entire select. causes return first 10 results including joined records. if first entity has 10 child objects result set return 1 entity 10 child objects loaded. need entities , child collections returned hydrated (lazy off). if turn lazy loading off , run query n+1 query each associate in result set.

this basic query process:

criteria = context.session.createcriteria<associate>(); criteria.setmaxresults(10); //hardcoded testing criteria.setfirstresult(1); //hardcoded testing 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");         //hasmany(x => x.directreports).element("managerid").collectiontype(typeof(domain.associate));       } 

the solution ended using subquery set max results. added subquery using subqueries.propertyin. cloning "criteria" "limiter" because added criterion expression in code not shown. need clone these criterion subquery top 10 select in "in" statement. can eager load child collections , add pagination root entity 10 enties without issues cartesian or n+1. try follow more complete , organized code.

//criteria = context.session.createcriteria<associate>();  //changed criteria detachedcriteria. criteria = detachedcriteria.for<associate>();  detachedcriteria limiter = criteriatransformer.clone(criteria);  limiter.setprojection(projections.id()); limiter.setmaxresults(10); criteria.add(subqueries.propertyin("associateid", limiter));  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>();  

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -