NHibernate collections: many-to-many relationships

Posted by Brad Heller on Stack Overflow See other posts from Stack Overflow or by Brad Heller
Published on 2010-06-13T23:42:20Z Indexed on 2010/06/13 23:52 UTC
Read the original article Hit count: 291

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate