NHibernate collections: many-to-many relationships
- by Brad Heller
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?