title: one-to-many mapping // how to map an association of legacy tables (Hibernate)
body: <p>I've got two  legacy database tables, layouted in simplified manner like this:</p>
  
  <pre><code>MASTER                        SLAVE
  ident                         ident
  sName                         sName
  sNumber                       sNumber
  sDesc                         sValue
  -----                         ------
  Java Class 'ScenarioMaster'   Java Class 'ScenarioSlave'
  </code></pre>
  
  <p>Each row has a surrogate index via the <code>ident</code> column, but there is no relationship between <code>MASTER.key</code> and <code>SLAVE.key</code>. However, for the <code>MASTER</code> table, uniqueness of one sName/sNumber pair is true. This would therefore be a possible and meaningful composite key (I do know that those are evil).</p>
  
  <p>In fact, there is a 1:n relationship meaning each <code>MASTER</code> row references <em>n</em> rows in the <code>SLAVE</code> table. Given the column description from above, possible population could be</p>
  
  <pre><code>MASTER                        SLAVE
  100                           42
  'labl'                        'labl'
  1                             1
  'some label'                  0.1
                                43
                               'labl'
                                1
                                0.2
  </code></pre>
  
  <p>Now, using hibernate, how could I reflect this relationship in my java classes? In <code>ScenarioMaster</code> I would declare a Set or List with public getters/setters like</p>
  
  <pre><code>private List&lt;ScenarioSlave&gt; slaves = new ArrayList&lt;ScenarioSlave&gt;();
  </code></pre>
  
  <p>The hibernate-mappings for <code>ScenarioMaster</code> could contain</p>
  
  <pre><code>&lt;bag name="slaves" cascade="all"&gt;
      &lt;key&gt;
          &lt;column name="sName" not-null="true"/&gt;
          &lt;column name="sNumber" not-null="true"/&gt;
      &lt;/key&gt;
      &lt;one-to-many class="ScenarioSlave"/&gt;
  &lt;/bag&gt;
  </code></pre>
  
  <p>This, however, creates a strage update statement when updating an already persitant <code>ScenarioMaster</code> entity using <code>session.saveOrUpdate(scenarioMaster)</code>.</p>
  
  <pre><code>// create master scn and slave slv      
  scn.addSlave(slv);  
  session.saveOrUpdate(scn);
  
      Hibernate: 
          update
              SLAVE
          set
              sNumber=?,
              sName=?,
              sValue=?
          where
              sName=? 
              and sNumber=?
      Hibernate: 
          update
              SLAVE
          set
              sName=null,
              sNumber=null 
          where
              sName=? 
              and sNumber=?   
  </code></pre>
  
  <p>What am I doing wrong? Where is that second update coming from? 
  I guess it has something to do with sName/sNumber not being a key for <code>ScenarioSlave</code>. I can't quite figure out, why. </p>
  
  <p>Note that the sName/sNumber parameters do point to valid values and that the <code>ScenarioMaster</code> instance I want to persist via <code>saveOrUpdate</code> actually has a non-null <code>ScenarioSlave</code> instance in the <code>slaves</code> List.</p>
  
  <p><strong>EDIT</strong>: Composite key is deferred to a separate class using this mapping</p>
  
  <pre><code>&lt;composite-id name="keyId" class="ScenarioKeyId"&gt;
      &lt;key-property name="name" access="field" column="sName"
          type="string" length="20"/&gt;
      &lt;key-property name="number" access="field" column="sNumber"
          type="long"/&gt;
  &lt;/composite-id&gt;
  </code></pre>
  
  <p>I don't really want to create a table <code>NAMENUMBER_KEY</code> which maps <em>'labl', 1</em> to something like <em>'key_labl1'</em> which can then be used as <code>id</code> for hibernate. I suppose, this is what hibernate does (without making use of an actual table).</p>