tags:

views:

39

answers:

2

I've got two models, a Product model and a ShoppingCart model. The ShoppingCart model has a collection of products as a property called Products (List). Here is the mapping for my ShoppingCart model.

<class name="MyProject.ShoppingCart, MyProject" table="ShoppingCarts">
  <id name="Id" column="Id">
    <generator class="native" />
  </id>

  <many-to-one name="Company" class="MyProject.Company, MyProject" column="CompanyId" />
  <property name="ExternalId" column="GUID" generated="insert" />
  <property name="Name" column="Name" />
  <property name="Total" column="Total" />
  <property name="CreationDate" column="CreationDate" generated="insert" />
  <property name="UpdatedDate" column="UpdatedDate" generated="always" />

  <bag name="Products" table="ShoppingCartContents" lazy="false">
    <key column="ShoppingCartId" />
    <many-to-many column="ProductId" class="MyProjectMyProject.Product, MyProject" fetch="join" />
  </bag>
</class>

When I try to save to the DB, the ShoppingCart is saved, but the mapping rows in ShoppingCartContents aren't save, making me thing that there's an issue with the mapping. Where am I going wrong here?

A: 

Add the cascade property to the bag

<bag name="Products" table="ShoppingCartContents" lazy="false" cascade="all">

There are serveral options for the cascade, from NH doc:

cascade="all|none|save-update|delete|all-delete-orphan" 

HTH

Markust
A: 

You should peform changes in the transaction, otherwise nhibernate won't save changes in collections.

Sly