title: nHibernate one-to-many best practice persintence to avoid INSERT NULL
body: <p>I've a One to Many mapping.
  An entity with a collection persisted on two table 1:N</p>
  
  <p>This is the class and the mapping:</p>
  
  <pre><code>public class Test
      {
          public virtual string Id { get; set; }
          public virtual string Description { get; set; }
          public virtual IList&lt;TestItem&gt; Items { get; set; }
  
          public Test()
          {
              Items = new List&lt;TestItem&gt;();
          }
      }
  
      public class TestItem
      {
          public virtual string Id { get; set; }
          public virtual Test Test { get; set; }
          public virtual string ItemCode { get; set; }
          public virtual string ItemData { get; set; }
      }
  
      public class TestMap : ClassMapping&lt;Test&gt;
      {
          public TestMap()
          {
              Id(x =&gt; x.Id, m =&gt; m.Column("IDTest"));
  
              Bag(x =&gt; x.Items, c =&gt;
              {
                  c.Key(k =&gt;
                  {
                      k.NotNullable(true);
                      k.Column("IDTest");
                  });
                  c.Cascade(Cascade.DeleteOrphans);
                  c.Lazy(CollectionLazy.NoLazy);
                  c.Inverse(true);
              }, r =&gt; r.OneToMany(m =&gt;
              {
                  m.NotFound(NotFoundMode.Exception);
                  m.Class(typeof(TestItem));
              }));
          }
      }
  
      public class TestItemMap : ClassMapping&lt;TestItem&gt;
      {
          public TestItemMap()
          {
              Id(x =&gt; x.Id, m =&gt; m.Column("IDTestItem"));
  
              ManyToOne(x =&gt; x.Test, m =&gt;
              {
                  m.Column("IDTest");
                  m.NotNullable(false);
                  m.Lazy(LazyRelation.NoLazy);
              });
  
              Property(x =&gt; x.ItemCode);
              Property(x =&gt; x.ItemData);
          }
      }
  </code></pre>
  
  <p>And this is the code.
  If I remove the marked line I get the error.</p>
  
  <pre><code>var session = SessionFactory.OpenSession();
  
              using (var tr = session.BeginTransaction())
              {
                  var test = new Test();
                  test.Id = "T01";
                  test.Description = "Desc TEST 01";
  
                  session.SaveOrUpdate(test); // If Removed Get INSERT ERROR on TestItem
  
                  var item = new TestItem { Id = "NEW01", ItemCode = "A", Test = test, ItemData = "New T01A" };
                  test.Items.Add(item);
                  session.SaveOrUpdate(item);
  
                  session.SaveOrUpdate(test);
  
                  tr.Commit();
              }
  </code></pre>
  
  <p>My question is:</p>
  
  <p>Is this the best practice to persist a "one to many" relation ?</p>
  
  <p>It is possible to use the code below and save all only saving the header Row (test) and automatically cascading all inserts on the child table??</p>
  
  <pre><code>var session = SessionFactory.OpenSession();
  
              using (var tr = session.BeginTransaction())
              {
                  var test = new Test();
                  test.Id = "T01";
                  test.Description = "Desc TEST 01";
  
                  //session.SaveOrUpdate(test); // If Removed Get INSERT ERROR on TestItem
  
                  var item = new TestItem { Id = "NEW01", ItemCode = "A", Test = test, ItemData = "New T01A" };
                  test.Items.Add(item);
                  //session.SaveOrUpdate(item);
  
                  session.SaveOrUpdate(test);
  
                  tr.Commit();
              }
  </code></pre>
  
  <p>I suppose I need to change something on my mapping code but i do not undestand what!!!</p>
  
  <p>Many days of work, googling, stackoverflowing but nothing change the result.</p>
  
  <p>Thank you!!!</p>