views:

4729

answers:

8

Hello,

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.

Within my object, I create a new ShoppingCart object. ShoppingCart cart = CreateOrGetCart();

I then add an existing Product which I got from the database to the start.

cart.AddItem(product);

This is just a simple wrapper to add the item to the IList.

    public virtual void AddItem(Product product)
    {
        Items.Add(new ShoppingCartItem { Quantity = 1, Product = product });
    }

I then call SaveOrUpdate on the Repository

Repository.SaveOrUpdate(cart);

Which looks like this:

   public T SaveOrUpdate(T entity)
    {
        Session.SaveOrUpdate(entity);
        return entity;
    }

I'm using Fluent NHibernate for the mapping:

    public ShoppingCartItemMap()
    {
        WithTable("ShoppingCartItems");

        Id(x => x.ID, "ShoppingCartItemId");
        Map(x => x.Quantity);

        References(x => x.Cart, "ShoppingCartId").Cascade.SaveUpdate();
        References(x => x.Product, "ProductId");
    }


    public ShoppingCartMap()
    {
        WithTable("ShoppingCarts");

        Id(x => x.ID, "ShoppingCartId");
        Map(x => x.Created);
        Map(x => x.Username);

        HasMany<ShoppingCartItem>(x => x.Items)
            .IsInverse().Cascade.SaveUpdate()
            .WithKeyColumn("ShoppingCartId")
            .AsBag();
    }

Database Schema (SQL Server 2005) is also fairly generic:

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

When I SaveOrUpdate my ShoppingCart, why isn't any ShoppingCartItems also being saved?

Please help.

Thanks

Ben

UPDATE: Wrapping it in a transaction providng me with some more info: {"Cannot insert the value NULL into column 'ShoppingCartId', table 'WroxPizza.dbo.ShoppingCartItems'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."} This is because it's a new cart.

+1  A: 

Try wrapping Session.SaveOrUpdate in a transaction, or force a flush directly after.

Quoo
A: 

I'm not sure, but I don't think that NHibernate cascades the saving of objects to child objects. You don't show the code that calls SaveOrUpdate, but I know if you loop through the ShoppingCartItems at that point, saving each individually then they'll be persisted too.

MrTelly
+2  A: 

Have you tried setting the Cart property of ShoppingCartItem in your AddItem method (with this)?

James Gregory
A: 

I know nothing about NHibernate (The OPF I use does this for me), but isn't it the case that when you save the aggregate root (ShoppingCart) that code should also commit its compositely owned instances to the session too?

Peter Morris
A: 

Is it possible that your cart items are saving first, then the cart? hence the child items have no parent ID to use?

You MAY need to explicitly save the cart back to the DB on create, then add the items into it and save - just to get the ID.

Nic Wise
+2  A: 

Why did you include .Cascade.SaveUpdate() in the line

References(x => x.Cart, "ShoppingCartId").Cascade.SaveUpdate()

?

Perhaps it confuses NHibernate (or Fluent NHibernate) that there seems to be cascading saves/updates from both ends of your relation?

Configuring your HasMany to cascade ought to be sufficient to achieve what you want.

mookid8000
+3  A: 

I had this exact problem with fluent nhibernate. There appears to be a small bug in mapping one-to-many relationships and the relationships not automatically saving or cascading. See here for a posting about it on the Fluent NHibernate group.

Apparently, there is someone working on the issue!

I can;t say for sure that this is the issue with your code but it looks very similar to a problem I had. Hope that helps some!

Jeffrey Cameron
A: 

Dude!

The problem is with your configuration statement of sessionfactory. Don't use Exposeconfiguration if u want ur data to persist. That will surely solve ur problem of data persistence.

Sushi Rocks [email protected]

use of schemaexport should be done carefully.