Note that there are some explanatory texts on larger screens.

plurals
  1. PODB2 Fluent NHibernate mapping duplicate records in HasMany mapping
    text
    copied!<p>I accessing a pre-existing database (actually DB2 on an IBM i), and have an issue with the mappings for the following (simple) structure in Fluent NHibernate. I have had to construct an artificial example, so forgive any ommissions.</p> <p>Job ...</p> <pre><code>public class Job { public virtual string JobCode { get; set; } public virtual string Owner{ get; set; } public virtual IList&lt;Deliverable&gt; Deliverables { get; set; } public Job() { Deliverables = new List&lt;Deliverable&gt;(); } } </code></pre> <p>Deliverable ..</p> <pre><code>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; } } </code></pre> <p>I am trying to map a 'HasMany' relationship between Job and Deliverable, as follows ..</p> <pre><code>public class JobMap : ClassMap&lt;Job&gt; { public JobMap() { Table("JOB"); Id(x =&gt; x.JobCode).Column("CODE"); Map(x =&gt; x.Owner).Column("WHODO"); HasMany(x =&gt; x.Deliverables) .KeyColumn("CODE"); } } public class DeliverableMap : ClassMap&lt;Deliverable&gt; { public DeliverableMap() { Table("DELIVERABLE"); Id(x =&gt; x.JobCode).Column("CODE"); Map(x =&gt; x.Reference).Column("UNQREF"); Map(x =&gt; x.Owner).Column("WHODO"); References( x =&gt; x.Job) .Column("CODE") ; } } </code></pre> <p>This seems to work, and if you take the SQL that is produced, and run it directly, the correct results are returned (in this case 11 records, all unique). BUT when I do the following, the list of Deliverables has 11 entries all identical.</p> <p>IList results = session .CreateCriteria(typeof(Job)) .Add(Expression.Eq("Code", "206171")) .List();</p> <pre><code>foreach (var job in results) { Console.WriteLine("job.JobCode" + job.JobCode); Console.WriteLine("job.Owner" + job.Owner); foreach (var deliverable in job.Deliverables) { **// These are all identical!** Console.WriteLine(deliverable.Reference); Console.WriteLine("deliverable.Owner" + deliverable.Owner); Console.WriteLine(deliverable.JobNumber); Console.WriteLine(deliverable.DeliverableTyoe); Console.WriteLine(deliverable.Description); } } </code></pre> <p>So, are the mappings incorrect, or is there something up with the way that I am using them?</p> <p>Many thanks in advance, I have been staring at this all day.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload