the following happens with a many-to-many relation
when i add a new product to an order, a SaveOrUpdate works fine.
(although i have to save both product & order. Only when i save Product, the OrderRegel table is filled. When Order is saved, no insert/update statements hit the DB. Is this normal behaviour?)
The main problem:
when we add an existing product to an order, a SaveOrUpdate does not do anything. (not passing insert or update statements to the DB)
Any help would be really appreciated with this.
i have the following tables:
Order >- OrderRegel -< Product
With the following classes
Order >-< Products
Code sample 1: new Product (Works)
Code:
Order order = new OrdersDao().GetById(23);
Product newProduct = new Product();
newProduct.Name = "Test";
order.Products.Add(newProduct);
newProduct.Orders.Add(order);
order.OrderDate = DateTime.Now;
new ProductDao().SaveOrUpdate(newProduct);
new OrderDao().SaveOrUpdate(order);
Code sample 2: existing product (does not work)
Code:
Order order = new OrdersDao().GetById(23);
Product existingProduct = new ProductDao().GetById(1);
order.Products.Add(existingProduct);
existingProduct.Orders.Add(order);
order.OrderDate = DateTime.Now;
new ProductDao().SaveOrUpdate(existingProduct);
new OrderDao().SaveOrUpdate(order);
Our mapping:
Product:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="DomainModel.Product,DomainModel" table="[Product]">
<id name="Id" column="ProductId" type="Int64" unsaved-value="0">
<generator class="native"/>
</id>
<idbag name="Orders" table="OrderRegel" lazy="true" cascade="save-update" >
<collection-id column="Id" type="Guid" >
<generator class="guid" />
</collection-id>
<key column="ProductId"/>
<many-to-many class="DomainModel.Orders, DomainModel" column="OrdersId" />
</idbag>
<property column="[Name]" type="String" name="Name" length="50" />
</class>
</hibernate-mapping>
Order:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="DomainModel.Orders,DomainModel" table="[Orders]" lazy="true">
<id name="Id" column="OrdersId" type="Int64" unsaved-value="0">
<generator class="native"/>
</id>
<idbag name="Producten" table="OrderRegel" lazy="true" cascade="save-update" >
<collection-id column="Id" type="Guid" >
<generator class="guid" />
</collection-id>
<key column="OrdersId"/>
<many-to-many class="DomainModel.Product, DomainModel" column="ProductId" outer-join="true"/>
</idbag>
<many-to-one name="Customer" column="[Customer]" class="DomainModel.Customer,DomainModel" cascade="all" />
<property column="[OrderDate]" type="DateTime" name="OrderDate" not-null="true" />
</class>
</hibernate-mapping>