title: Insert into child table with NHibernate 2.0.1
body: <p>I started using <a href="http://en.wikipedia.org/wiki/NHibernate" rel="nofollow noreferrer">NHibernate</a> this week (struggling). I have a small application with 3 tables (I use 2 for now). Table <code>currency</code> and table <code>country</code> here are the mapping files.</p>
  
  <pre><code>&lt;class name="dataprovider.Country,dataprovider" table="country"&gt;
      &lt;id name="CountryId" column="country_id" unsaved-value="0"&gt;
          &lt;generator class="native"/&gt;
      &lt;/id&gt;
      &lt;!--&lt;bag name="BatchList" inverse="true" lazy="true" &gt;
          &lt;key column="country_id" /&gt;
          &lt;one-to-many class="Batch" /&gt;
      &lt;/bag&gt;
      &lt;bag name="PrinterList" inverse="true" lazy="true" &gt;
          &lt;key column="country_id" /&gt;
          &lt;one-to-many class="Printer" /&gt;
      &lt;/bag&gt;--&gt;
      &lt;many-to-one name="CurrencyId" column="currency_id" class="Currency"  cascade="save-update"/&gt;
      &lt;!--&lt;property column="currency_id" name="Currency_Id"/&gt;--&gt;
      &lt;property column="name" name="Name"/&gt;
      &lt;property column="region" name="Region" /&gt;
      &lt;property column="comments" name="Comments"/&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>The currency mapping file:</p>
  
  <pre><code>&lt;class name="dataprovider.Currency, dataprovider" table="currency"&gt;
      &lt;id name="CurrencyId" column="currency_id" &gt;
          &lt;generator class="native"/&gt;
      &lt;/id&gt;
      &lt;bag name="CountryList" inverse="true" lazy="true" &gt;
          &lt;key column="currency_id" /&gt;
          &lt;one-to-many class="Country" /&gt;
      &lt;/bag&gt;
      &lt;!--&lt;bag name="DenominationList" inverse="true" lazy="true" &gt;
          &lt;key column="currency_id" /&gt;
          &lt;one-to-many class="Denomination" /&gt;
      &lt;/bag&gt;--&gt;
      &lt;property column="name" name="Name"/&gt;
      &lt;property column="authorizer" name="Authorizer"  /&gt;
      &lt;property column="date_created"  name="DateCreated" type="DateTime" not-null="true" /&gt;
      &lt;property column="comments"  name="Comments"  /&gt;
  </code></pre>
  
  <p>The many to one relationship that <code>country</code> hold create an attribute of the type <code>Currency</code> in the <code>country</code> persistence class. Now while my test <code>can_add_currency</code> and <code>can_add_country</code> succeeded (I export the schema) I have null value in the table <code>country</code> on the field <code>currency_id</code>.</p>
  
  <p>Here is the test code:</p>
  
  <pre><code>[Test]
      public void can_add_new_country()
      {
          CountryManager cm = new CountryManager();
  
          Country country = new Country();
          //country.CurrencyId = CurrencyManager.GetCurrencyById(1);
          country.CurrencyId = new CurrencyManager().GetCurrencyById(1);
          country.Name = "POUND";
          country.Region = "ENGLAND";
          country.Comments = "the comment";
  
          cm.Add(country);
  
          using(ISession session = _sessionFactory.OpenSession())
          {
              Country fromdb = session.Get&lt;Country&gt;(country.CountryId);
              Assert.IsNotNull(fromdb);
              Assert.AreNotSame(fromdb, country);
          }
      }
  
      public Currency GetCurrencyById(int currency_id)
      {//GetCurrencyById from CurrencyManger class
          try
          {
              using(ISession session = NHibernateHelper.OpenSession())
              {
                  return session.Get&lt;Currency&gt;(currency_id);
  
              }
          } catch (Exception ex)
          {
              return null;
          }
      }
  </code></pre>
  
  <p>The question is: <strong>how to insert into table country with the currency_id of an existing currency_id from the table currency</strong>?</p>
  
  <p>How do you guys/gals do it? I'm seriously stuck and a 2 day small project is taking me one week now.</p>