Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>many-to-any</strong><br> If the table structure is fixed, and you want to use the <code>identity</code> id generator, I would map the Source tables as 3 separate classes and map to the common interface as an <a href="http://wiki.fluentnhibernate.org/Fluent_mapping#Any" rel="nofollow noreferrer">any</a> reference.</p> <pre><code>class Case { public virtual IReferral Referral { get; set; } } class SourceA : IReferral { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual string Type { get { return "SourceA"; } } } interface IReferral { int Id { get; set; } string Name { get; set; } string Type { get; } } public class CaseMap : ClassMap&lt;Case&gt; { public CaseMap() { ReferencesAny(m =&gt; m.Referral) .EntityTypeColumn("ReferralType") .EntityIdentifierColumn("ReferralId") .AddMetaValue&lt;SourceA&gt;("SourceA") .AddMetaValue&lt;SourceB&gt;("SourceB") .AddMetaValue&lt;SourceC&gt;("SourceC") .IdentityType&lt;int&gt;(); } } </code></pre> <p><strong>union-subclass</strong><br> Another option is <a href="http://nhforge.org/doc/nh/en/index.html#mapping-declaration-unionsubclass" rel="nofollow noreferrer">union-subclass</a> with an abstract base class mapping. This allows eager fetching, but you cannot use the <code>identity</code> generator on the subclass tables.</p> <pre><code>&lt;class name="IReferral" abstract="true" table="Referral"&gt; &lt;id name="Id"&gt; &lt;generator class="hilo"/&gt; &lt;/id&gt; &lt;property name="Name"/&gt; &lt;union-subclass name="SourceA" table="SourceA"&gt; &lt;!-- class specific properties --&gt; &lt;/union-subclass&gt; &lt;union-subclass name="SourceB" table="SourceB"&gt; &lt;!-- class specific properties --&gt; &lt;/union-subclass&gt; &lt;union-subclass name="SourceC" table="SourceC"&gt; &lt;!-- class specific properties --&gt; &lt;/union-subclass&gt; &lt;/class&gt; </code></pre> <p><strong>subclass</strong><br> If you can change the tables, you can map all 3 Referral classes to the same table using <a href="http://nhforge.org/doc/nh/en/index.html#mapping-declaration-subclass" rel="nofollow noreferrer">subclass</a>.</p> <pre><code>&lt;class name="IReferral" abstract="true" table="Referral" discriminator-value="null"&gt; &lt;id name="Id"&gt; &lt;generator class="identity"/&gt; &lt;/id&gt; &lt;discriminator column="Discriminator" not-null="true" type="System.String"/&gt; &lt;property name="Name"/&gt; &lt;subclass name="SourceA" discriminator-value="SourceA"&gt; &lt;!-- class specific properties --&gt; &lt;/subclass&gt; &lt;subclass name="SourceB" discriminator-value="SourceB"&gt; &lt;!-- class specific properties --&gt; &lt;/subclass&gt; &lt;subclass name="SourceC" discriminator-value="SourceC"&gt; &lt;!-- class specific properties --&gt; &lt;/subclass&gt; &lt;/class&gt; </code></pre>
 

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