c# - How can I eagerly load a child collection in nhibernate -
apologies such noob question.
i have 2 entities: parent , child, modeled thusly:
public class parent() { public virtual int id{ get; protected set; }, public virtual ilist<child> children {get;set;} } public class child() { public virtual int id{ get; protected set; }, public virtual parent parent {get;set;} }
with these mappings:
public class parentmap : classmap<parent> { public parentmap() { id(x => x.id).generatedby.identity(); hasmany(x=>x.children) .cascade .alldeleteorphan() .inverse(); } } public class childmap : classmap<child> { public childmap() { id(x => x.id).generatedby.identity(); references(x=>x.parent) .cascade .saveupdate(); } }
i have left lazy loading on, parent can have many children , don't want load children.
i have tried following methods:
var query1 = session.queryover<parent>() .where(x => x.id == parent.id) .fetch(t=>t.children).eager().list<parent>(); var query2 = session.createcriteria<parent>("d") .add(restrictions.eq("id", parent.id)) .setfetchmode("d.children", fetchmode.eager) .list<parent>(); var query3 = session.createcriteria(typeof (parent)) .add(restrictions.eq("id", parent.id)) .setfetchmode("children", fetchmode.eager) .list<parent>();
i've simplified objects, of course, i've tried these in own console app, test. problem is: in none of queries children.
looking @ sql profiler, cannot see join being generated nhibernate. know can go other way , parent children, i'd rather not that.
i think missing fundamental in understanding of nhibernate.
thanks help
resolved this, , should have included more details in first code example.
i tried retrieve child objects of parent after saving parent(with cascade) in same session.
closing session , trying 1 of above queries returned child(ren).
Comments
Post a Comment