i'm trying fetch collection of read-only objects nhibernate properties come single table (answers), exception of 1 property comes table (questions) in many 1 relationship. fact it's 2 tables implementation detail want hide, want repository return sensible aggregate. trouble requires have 2 classes, 1 each table, nhibernate returns , have select/map third class repository returns. feels bit rubbish instead hoping have mapping joins 2 tables me maps columns onto single class. mapping looks this:
public questionanswermap() { readonly(); table("question"); id(x => x.id).column("questionid").generatedby.identity(); map(x => x.answershortcode).column("answershortcode"); join("answers", join => { join.fetch.join(); join.keycolumn("questionid").inverse(); join.map(x => x.questionid).column("questionid"); join.map(x => x.answerid).column("answerid"); join.map(x => x.memberid).column("memberid"); }); } the sql generates looks perfect , returns want, when there multiple answers join same row in questions table, nhibernate seems map them objects wrongly - right number of results, answers have common question hydrated first row in sql result question.
am doing right way? nh generating right sql, why building objects wrong?
because join meant way. assumes 1 one association between 2 tables not case.
instead of mapped entity prefere on fly dto this:
var query = session.query<answer>() .where(answer => ...) .select(answer => new questionanswer { questionid = answer.question.id, answershortcode = answer.question.answershortcode, answerid = answer.id, memberid = answer.memberid, }); return query.tolist();
Comments
Post a Comment