title: Referential Integrity on NHibernate with an Abstract Class
body: <p>I have this situation, a Product base class which is abstract and two concrete subclasses (Car, Bike). Then an Invoice which owns a collection of InvoiceItem. InvoiceItem reference a Product instance.
  Here is the hbms:</p>
  <p>Product Class</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  &lt;hibernate-mapping xmlns=&quot;urn:nhibernate-mapping-2.2&quot;
                 assembly=&quot;MyAssembly&quot;
                 namespace=&quot;MyNamespace&quot;&gt;
  &lt;class name=&quot;Product&quot; abstract=&quot;true&quot;&gt;
      &lt;id name=&quot;Id&quot; column=&quot;id&quot;&gt;
          &lt;generator class=&quot;guid.comb&quot; /&gt;
      &lt;/id&gt;
      &lt;property name=&quot;Code&quot; column=&quot;code&quot; type=&quot;string&quot; not-null=&quot;true&quot; /&gt;
      &lt;property name=&quot;Name&quot; column=&quot;name&quot; type=&quot;string&quot; not-null=&quot;true&quot; /&gt;
      
      &lt;set name=&quot;Items&quot; inverse=&quot;true&quot;&gt;
          &lt;key column=&quot;productId&quot; /&gt;
          &lt;one-to-many class=&quot;InvoiceItem&quot;/&gt;
      &lt;/set&gt;
      &lt;union-subclass name=&quot;Car&quot; table=&quot;Cars&quot;&gt;
          &lt;property name=&quot;EngineName&quot; column=&quot;engineName&quot; not-null=&quot;true&quot; /&gt;
          &lt;/union-subclass&gt;
          &lt;union-subclass name=&quot;Bike&quot; table=&quot;Bikes&quot;&gt;
              &lt;property name=&quot;ShifterSpeedCount&quot; column=&quot;shifterSpeedCount&quot; type=&quot;int&quot; not-null=&quot;true&quot; /&gt;
          &lt;/union-subclass&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  <p>InvoiceItem Class</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  &lt;hibernate-mapping xmlns=&quot;urn:nhibernate-mapping-2.2&quot;
                 assembly=&quot;MyAssembly&quot;
                 namespace=&quot;MyNamespace&quot;&gt;
  &lt;class name=&quot;InvoiceItem&quot; table=&quot;InvoiceItems&quot;&gt;
      &lt;id name=&quot;Id&quot; column=&quot;id&quot;&gt;
          &lt;generator class=&quot;guid.comb&quot; /&gt;
      &lt;/id&gt;
  
      &lt;property name=&quot;Price&quot; column=&quot;price&quot; precision=&quot;15&quot; scale=&quot;3&quot; not-null=&quot;true&quot; /&gt;
      &lt;property name=&quot;Count&quot; column=&quot;count&quot; type=&quot;int&quot; not-null=&quot;true&quot; /&gt;
      &lt;many-to-one name=&quot;Product&quot; column=&quot;productId&quot; foreign-key=&quot;FK_ProductInvoiceItems&quot;  /&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  <p>The problem I have is that when I Delete an instance of Car, NHibernate doesn't checks the integrity in InvoiceItem, so after the delete, I have a broken reference, causing problems.</p>
  <p>The problem is caused because of the inheritance, the productId column can't reference to a single table, causing that NHibernate doesn't generate the FK.</p>
  <p>This scenario is extremely simple, so may be check the reference manually is simple. But how can I solve this problem in a more complex project?
  Is there a way to force NHibernate to check the Integrity automatically?</p>