title: HasMany mapping with non-nullable key column
body: <p>I am trying to achieve a <code>HasMany</code> mapping with a non-nullable key column.</p>
  
  <p>My mapping looks like this:</p>
  
  <pre><code>public void Override(AutoMapping&lt;DataExportTable&gt; mapping)
  {
      mapping.Table("data_export_tables");
      mapping.Id(x =&gt; x.Id).Column("data_export_tables_id")
             .GeneratedBy.Sequence("SQ_DATA_EXPORT_TABLES_ID");
      mapping.HasMany(x =&gt; x.Columns)
             .KeyColumn("table_id")
             .Not.KeyNullable()
             .Not.KeyUpdate()
             .Cascade.AllDeleteOrphan();
  }
  </code></pre>
  
  <p>The code to save the entity looks like this:</p>
  
  <pre><code>var table = new DataExportTable("table_name");
  table.Columns.Add(new DataExportColumn("column_name"));
  session.Save(table);
  transaction.Commit();
  </code></pre>
  
  <p>This throws an exception:</p>
  
  <blockquote>
    <p>NHibernate.Exceptions.GenericADOException: could not execute batch command.[SQL: SQL not available] ---> Oracle.DataAccess.Client.Orac
    leException: ORA-01400: cannot insert NULL into ("MYSCHEMA"."DATA_EXPORT_COLUMNS"."TABLE_ID")</p>
  </blockquote>
  
  <p>Now, I read quite a few posts about this topic and the generally accepted solution seems to be to make the reference bi-directional, i.e. adding a <code>Table</code> property to <code>DataExportColumn</code> and setting that.<br>
  Another "hack" is to make the foreign key column nullable.
  That's not what I want. I want NHibernate to insert the ID directly in the INSERT statement.<br>
  According to the log file, NHibernate certainly knows the ID at the time it executes the INSERT statement:</p>
  
  <blockquote>
    <p>NHibernate: select SQ_DATA_EXPORT_TABLES_ID.nextval from dual<br>
    NHibernate: Batch commands:<br>
    command 0:INSERT INTO data_export_tables (NAME, data_export_tables_id) VALUES (:p0, :p1);:p0 = 'table_name' [Type: String (0)], :p1 = 93 [Type: Int32 (0)]</p>
    
    <p>NHibernate: Batch commands:<br>
    command 0:INSERT INTO data_export_columns (NAME, data_export_columns_id) VALUES (:p0, :p1);:p0 =
    'column_name' [Type: String (0)], :p1 = 1228 [Type: Int32 (0)]</p>
  </blockquote>
  
  <p>As you can see, NHibernate simply ommits the <code>table_id</code> column, although the ID is well known - NHibernate passed it as a parameter to the first insert statement...</p>