title: How to map (and configure) a circular reference in NHibernate?
body: <p>I have 2 tables that hold Office Locations and Computers: <code>tblLocation</code> and <code>tblMachine</code> respectively. Each of these tables reference the other. Each Location has multiple computers (machines). Each Location can send display information to any machine (regardless of location). One Machine could be the Display Machine of many locations. </p>
  
  <p>I have the following setup using NHibernate as the ORM.</p>
  
  <p>Simplified example of my tables:</p>
  
  <ul>
  <li><code>tblLocation</code> has columns <code>LocationId</code> (pk), <code>LocationName</code>, <code>DisplayMachineId</code> (fk)</li>
  <li><code>tblMachine</code> has columns <code>MachineId</code> (pk), <code>MachineName</code>, <code>LocationId</code> (fk)</li>
  </ul>
  
  <p>I also have Location and Machine objects like this:</p>
  
  <pre><code>public class Location
  {
      public virtual int LocationId { get; set; }
      public virtual string Name { get; set; }
      public virtual Machine DisplayMachine { get; set; }
      public virtual ISet&lt;Machine&gt; Machines { get; set; }
  }
  
  public class Machine
  {
      public virtual int MachineId { get; set; }
      public virtual Location Location { get; set; }
      public virtual ISet&lt;Location&gt; Locations { get; set; }
  }
  </code></pre>
  
  <p>Here are the mapping files for each:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;class name="DataTransfer.Machine,DataTransfer" table="tblMachine"&gt;
  &lt;id name="MachineId" column="MachineID" type="Int32" unsaved-value="0"&gt;
    &lt;generator class="native"/&gt;
  &lt;/id&gt;
  &lt;property name="Name" column="MachineName" type="string" not-null="true" /&gt;
  &lt;many-to-one name="Location" column="LocationID" not-null="false" class="DataTransfer.Location,DataTransfer" /&gt;
  &lt;set name="Locations" table="tblLocation" generic="true" inverse="true"&gt;
    &lt;key column="DisplayFromMachineID" /&gt;
    &lt;one-to-many class="DataTransfer.Location,DataTransfer"/&gt;
  &lt;/set&gt;
  &lt;set name="MachineReportProperties" table="tblMachineReportProperty" generic="true" inverse="true"&gt;
    &lt;key column="MachineID" /&gt;
    &lt;one-to-many class="DataTransfer.MachineReportProperty,DataTransfer"/&gt;
  &lt;/set&gt;
  &lt;/class&gt;
  
  &lt;class name="DataTransfer.Location,DataTransfer" table="tblLocation"&gt;
  &lt;id name="LocationId" column="LocationID" type="Int32" unsaved-value="0"&gt;
    &lt;generator class="native"/&gt;
  &lt;/id&gt;
  &lt;property name="Name" column="LocationName" type="string" not-null="true" /&gt;
  &lt;many-to-one name="DisplayMachine" column="DisplayFromMachineID" not-null="false" class="DataTransfer.Machine,DataTransfer" /&gt;
  &lt;set name="Machines" table="tblMachine" generic="true" inverse="true"&gt;
    &lt;key column="LocationID" /&gt;
    &lt;one-to-many class="DataTransfer.Machine,DataTransfer"/&gt;
  &lt;/set&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>I am receiving a foreign key violation when attempting to query the database. </p>
  
  <p>I have tried to start over and recreate the mappings and objects/properties, thinking I may have incorrectly configured them. Is there anything obvious that I have missed in this setup? I realize that ultimately the answer may be to change the database structure, I just wanted to know if there is anything I missed.</p>
  
  <p>Note: when I remove the relation in the database from <code>tblMachine.MachineId</code> (pk) to <code>tblLocation.DisplayMachineId</code> the fk violation goes away.</p>