c# - How to partially project a child object with many fields in nHibernate -
i have next nhibernate query select course of study based on course of study id , homecoming selected fields course of study object on initial fetch, , query executes no issues.
matchmode alternative = ... courseitem coursealias = null; teacheritem teacheralias = null; var query = session.queryover<courseitem>() .joinalias(c => c.teacher, () => teacheralias) .where(c => c.courseid.isinsensitivelike(strnumber, option)) .selectlist(list => list .select(c => c.courseid).withalias(() => coursealias.courseid) .select(c => c.isactive).withalias(() => coursealias.isactive) .select(c => c.coursedesc).withalias(() => coursealias.coursedesc) .select(c => c.teacher).withalias(() => coursealias.teacher)) .transformusing(transformers.aliastobean<courseitem>()) .list<courseitem>();
i wanted go step farther query homecoming partial teacher object, let's wanted homecoming id , name. so, updated projected list follows:
var query = session.queryover<courseitem>() .joinalias(c => c.teacher, () => teacheralias) .where(c => c.courseid.isinsensitivelike(strnumber, option)) .selectlist(list => list .select(c => c.courseid).withalias(() => coursealias.courseid) .select(c => c.isactive).withalias(() => coursealias.isactive) .select(c => c.coursedesc).withalias(() => coursealias.coursedesc) .select(c => c.teacher.id).withalias(() => coursealias.teacher.id) .select(c => c.teacher.name).withalias(() => coursealias.teacher.name)) .transformusing(transformers.aliastobean<courseitem>()) .list<courseitem>();
the query doesn't work because nhibernate has no thought how resovled based on teacher.id , teacher.name. thoughts on whether it's possible not fetch entire kid object parent object?
i've tried next query , works not desired outcome
var query = session.queryover<courseitem>(() => coursealias) .joinalias(() => coursealias.teacher, () => teacheralias) .where(() => coursealias.courseid.isinsensitivelike(strnumber, option)) .selectlist(list => list .select(() => coursealias.courseid) .select(() => coursealias.isactive) .select(() => coursealias.coursedesc) .select(() => teacher.id) .select(() => teacher.name)) .list<object[]>();
i can query right values unable transform correctly course of study / teacher info type.
any thoughts?
thanks!
we can indeed utilize custom transformer. there one, using very deep projections (inlcuding dynamic objects - 5.1.13. component, dynamic-component)
deeptransformer<tentity>
take (if needed adjust it) , final query this
// lastly lines different var query = session.queryover<courseitem>() .joinalias(c => c.teacher, () => teacheralias) .where(c => c.courseid.isinsensitivelike(strnumber, option)) .selectlist(list => list .select(c => c.courseid).withalias(() => coursealias.courseid) .select(c => c.isactive).withalias(() => coursealias.isactive) .select(c => c.coursedesc).withalias(() => coursealias.coursedesc) // native witalias not work, uses look // extract lastly property //.select(c => c.teacher.id).withalias(() => coursealias.teacher.id) //.select(c => c.teacher.name).withalias(() => coursealias.teacher.name)) // can utilize way pass deep alias .select(projections.property(() => teacheralias.id).as("teacher.id")) .select(projections.property(() => teacheralias.name).as("teacher.name")) // instead of // .transformusing(transformers.aliastobean<courseitem>()) // utilize .transformusing(new deeptransformer<courseitem>())
and in case, aliases match property names, transformer built object tree...
c# nhibernate
No comments:
Post a Comment