Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate mapping - one-to-one (or one-to-zero)
    primarykey
    data
    text
    <p>NHibernatians!</p> <p>I have a table [dbo].[Wibble] and another table [dbo].[WibbleExtended].</p> <p>[Wibble] is the main table and [WibbleExtended] is an optional table where some other fields are stored. There are far fewer entries in the [WibbleExtended] table than the main [Wibble] table. I think this was done back in the day to cure some space issues (Wibble has many rows and WibbleExtened has many columns).</p> <p>The ID for each table is the same and comes from an external source. </p> <p>I.e.</p> <pre><code>[dbo].[Wibble].[WibbleId] </code></pre> <p>and </p> <pre><code>[dbo].[WibbleExtended].[WibbleId] </code></pre> <p>are identical and is how the two tables relate.</p> <p>N.B. I can't change the schema. I'm shoe-horning this onto a legacy system that I have almost no control over.</p> <p>Searching around it seems that one-to-one mappings are problematic and the prevailing wisdom is to use two many-to-one mappings.</p> <p>My mappings currently are:</p> <pre><code>&lt;class name="Wibble" table="Wibble" &gt; &lt;id name="Id" column="WibbleId" type="Int32"&gt; &lt;generator class="assigned"/&gt; &lt;/id&gt; &lt;many-to-one name="WibbleExtended" class="WibbleExtended" column="WibbleId" not-null="false" cascade="all"/&gt; &lt;/class&gt; </code></pre> <p>And</p> <pre><code>&lt;class name="WibbleExtended" table="WibbleExtended" &gt; &lt;id name="Id" column="WibbleId" type="Int32"&gt; &lt;generator class="assigned" /&gt; &lt;/id&gt; &lt;many-to-one name="Wibble" class="Wibble" column="WibbleId" not-null="true" /&gt; &lt;/class&gt; </code></pre> <p>The problem with this is I'm getting errors such as </p> <pre><code>System.IndexOutOfRangeException: Invalid index n for this SqlParameterCollection with Count=n. </code></pre> <p>I've looked around and this does look like the correct strategy, it's just falling at the final hurdle.</p> <p>Is the problem the id generator? Other aspect of the mapping?</p> <p>Free mince pie for the correct answer.</p> <p>EDIT: Ok - here's what I did to solve this via @James Gregory.</p> <ol> <li><p>Moved the unit tests from the WibbleExtended tests to the Wibble test class and made the necessary modifications.</p></li> <li><p>Added the following to the Wibble.hbm.xml </p> <pre><code>&lt;join table="WibbleExtended" optional="true"&gt; &lt;key column="WibbleId"/&gt; &lt;property name="Blah1" column="Blah1" type="String" length="2000" not-null="false" /&gt; &lt;property name="Blah2" column="Blah2" type="String" length="1000" not-null="false" /&gt; &lt;/join&gt; </code></pre></li> <li><p>Added the corresponding properties to the Wibble POCO.</p></li> <li><p>Deleted all code relating to WibbleExtended.</p></li> <li><p>Run tests, all passed, checked in. Build passed. Went for an xmas beer (hence it's been a couple of days before I updated this! :-))</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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