DB2 Fluent NHibernate mapping duplicate records in HasMany mapping -
i accessing pre-existing database (actually db2 on ibm i), , have issue mappings following (simple) structure in fluent nhibernate. have had construct artificial example, forgive ommissions.
job ...
public class job { public virtual string jobcode { get; set; } public virtual string owner{ get; set; } public virtual ilist<deliverable> deliverables { get; set; } public job() { deliverables = new list<deliverable>(); } }
deliverable ..
public class deliverable { public virtual string jobcode { get; set; } public virtual int package { get; set; } public virtual string owner { get; set; } public virtual string reference { get; set; } public virtual job job { get; set; } }
i trying map 'hasmany' relationship between job , deliverable, follows ..
public class jobmap : classmap<job> { public jobmap() { table("job"); id(x => x.jobcode).column("code"); map(x => x.owner).column("whodo"); hasmany(x => x.deliverables) .keycolumn("code"); } } public class deliverablemap : classmap<deliverable> { public deliverablemap() { table("deliverable"); id(x => x.jobcode).column("code"); map(x => x.reference).column("unqref"); map(x => x.owner).column("whodo"); references( x => x.job) .column("code") ; } }
this seems work, , if take sql produced, , run directly, correct results returned (in case 11 records, unique). when following, list of deliverables has 11 entries identical.
ilist results = session .createcriteria(typeof(job)) .add(expression.eq("code", "206171")) .list();
foreach (var job in results) { console.writeline("job.jobcode" + job.jobcode); console.writeline("job.owner" + job.owner); foreach (var deliverable in job.deliverables) { **// these identical!** console.writeline(deliverable.reference); console.writeline("deliverable.owner" + deliverable.owner); console.writeline(deliverable.jobnumber); console.writeline(deliverable.deliverabletyoe); console.writeline(deliverable.description); } }
so, mappings incorrect, or there way using them?
many in advance, have been staring @ day.
i seem have fixed it. added compositeid deliverables mapping
compositeid() .keyproperty(x => x.jobcode, "code") .keyproperty(x => x.reference, "unqref");
this meant had override following in deliverable class
public override bool equals(object obj) { if (obj == null) return false; var t = obj deliverable; if (t == null) return false; if (jobcode == t.jobcode && reference == t.reference) return true; return false; } public override int gethashcode() { return (jobcode + "|" + reference).gethashcode(); }
and changed job mapping below
hasmany(x => x.deliverables) .keycolumn("codex") .inverse() .cascade.all();
i not sure of these has corrected situation (i suspect .inverse()
in job mapping.
i not sure generated sql looks like, answers correct.
Comments
Post a Comment