title: NHibernate: Using Fluent Nhibernate to save child objects
body: <p>In my system, I have two entities - ShoppingCart and ShoppingCartItem. Fairly generic use-case. However, when I save my ShoppingCart, none of the items are being saved to the DB. </p>
  
  <p>Within my object, I create a new ShoppingCart object.</p>
  
  <pre><code>ShoppingCart cart = CreateOrGetCart();
  </code></pre>
  
  <p>I then add an existing Product which I got from the database to the start. </p>
  
  <pre><code>cart.AddItem(product);
  </code></pre>
  
  <p>This is just a simple wrapper to add the item to the IList.</p>
  
  <pre><code>    public virtual void AddItem(Product product)
      {
          Items.Add(new ShoppingCartItem { Quantity = 1, Product = product });
      }
  </code></pre>
  
  <p>I then call SaveOrUpdate on the Repository</p>
  
  <pre><code>Repository.SaveOrUpdate(cart);
  </code></pre>
  
  <p>Which looks like this:</p>
  
  <pre><code>   public T SaveOrUpdate(T entity)
      {
          Session.SaveOrUpdate(entity);
          return entity;
      }
  </code></pre>
  
  <p>I'm using Fluent NHibernate for the mapping:</p>
  
  <pre><code>    public ShoppingCartItemMap()
      {
          WithTable("ShoppingCartItems");
  
          Id(x =&gt; x.ID, "ShoppingCartItemId");
          Map(x =&gt; x.Quantity);
  
          References(x =&gt; x.Cart, "ShoppingCartId").Cascade.SaveUpdate();
          References(x =&gt; x.Product, "ProductId");
      }
  
  
      public ShoppingCartMap()
      {
          WithTable("ShoppingCarts");
  
          Id(x =&gt; x.ID, "ShoppingCartId");
          Map(x =&gt; x.Created);
          Map(x =&gt; x.Username);
  
          HasMany&lt;ShoppingCartItem&gt;(x =&gt; x.Items)
              .IsInverse().Cascade.SaveUpdate()
              .WithKeyColumn("ShoppingCartId")
              .AsBag();
      }
  </code></pre>
  
  <p>Database Schema (SQL Server 2005) is also fairly generic:</p>
  
  <pre><code>CREATE TABLE [dbo].[ShoppingCarts]
  (
  [ShoppingCartID] [int] NOT NULL IDENTITY(1, 1),
  [Username] [nvarchar] (50) NOT NULL,
  [Created] [datetime] NOT NULL
  )
  GO
  ALTER TABLE [dbo].[ShoppingCarts] ADD CONSTRAINT [PK_ShoppingCarts] PRIMARY KEY CLUSTERED ([ShoppingCartID])
  GO
  
  
  
  CREATE TABLE [dbo].[ShoppingCartItems]
  (
  [ShoppingCartItemId] [int] NOT NULL IDENTITY(1, 1),
  [ShoppingCartId] [int] NOT NULL,
  [ProductId] [int] NOT NULL,
  [Quantity] [int] NOT NULL
  )
  GO
  ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [PK_ShoppingCartItems] PRIMARY KEY CLUSTERED ([ShoppingCartItemId])
  GO
  ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [FK_ShoppingCartItems_Products] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Products] ([ProductId])
  GO
  ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [FK_ShoppingCartItems_ShoppingCarts] FOREIGN KEY ([ShoppingCartId]) REFERENCES [dbo].[ShoppingCarts] ([ShoppingCartID])
  GO
  </code></pre>
  
  <p>When I SaveOrUpdate my ShoppingCart, why isn't any ShoppingCartItems also being saved? </p>
  
  <p>Please help.</p>
  
  <p>Thanks</p>
  
  <p>Ben</p>
  
  <p>UPDATE:
  Wrapping it in a transaction providng me with some more info:</p>
  
  <blockquote>
    <p>Cannot insert the value NULL into column 'ShoppingCartId', table
    'WroxPizza.dbo.ShoppingCartItems'; column does not allow nulls.
    INSERT fails. The statement has been terminated.</p>
  </blockquote>
  
  <p>This is because it's a new cart.</p>