I have 4 persistent classes:
-Customer
-Order
-OrderLine
-Article
A customer has orders, an Order has OrderLines and an OrderLine has a reference to an ARticle.
Now, When I want to add a new order, I do it like this (in short):
Order o = new Order();
theCustomer.AddOrder(order);
o.AddOrderLine (orderLine1);
mySession.SaveOrUpdateCopy (theCustomer)
NHibernate generates 3 queries:
These 2, which are correct:
Code:
exec sp_executesql N'INSERT INTO tblOrder (CustomerId, OrderDate) VALUES (@p0, @p1); select SCOPE_IDENTITY()', N'@p0 int,@p1 datetime', @p0 = 2, @p1 = 'Mar 11 2006 5:10:50:000PM'
exec sp_executesql N'INSERT INTO tblOrderLine (NumberOfItems, ItemPrice, ArticleId, OrderId) VALUES (@p0, @p1, @p2, @p3); select SCOPE_IDENTITY()', N'@p0 int,@p1 decimal(19,5),@p2 int,@p3 int', @p0 = 1, @p1 = 874.00000, @p2 = 2, @p3 = 5
But, after these 2 insert queries, nhibernate also issues an update statement, and I do not understand why it does that. This update-statement should not occur, since it is trying to update the primary key of the order-table:
Code:
exec sp_executesql N'UPDATE tblOrder SET Id = @p0 WHERE Id = @p1', N'@p0 int,@p1 int', @p0 = 2, @p1 = 5
Anybody out there who has a clue why he's doing this ?
Is there something wrong with my mapping file ?
Code:
<class name="ShopDomain.Order, ShopDomain" table="tblOrder">
<id name="id" type="Int32" access="field" column="Id" unsaved-value="-1" >
<generator class="identity"/>
</id>
<property name="OrderDate" column="OrderDate" />
<many-to-one name="Owner" column="CustomerId"
class="ShopDomain.Customer, ShopDomain"
not-null="true"/>
<bag name="orderLines" cascade="save-update" access="field" lazy="true">
<key column="Id"/>
<one-to-many class="ShopDomain.OrderLine, ShopDomain"/>
</bag>
</class>
Code:
<class name="ShopDomain.OrderLine, ShopDomain" table="tblOrderLine">
<id name="id" type="Int32" column="Id" access="field" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="NumberOfItems" column="NumberOfItems"/>
<property name="Price" column="ItemPrice"/>
<many-to-one name="OrderedArticle" column="ArticleId"
class="ShopDomain.Article, ShopDomain"
not-null="true"/>
<many-to-one name="Owner" column="OrderId"
class="ShopDomain.Order, ShopDomain"
not-null="true"/>
</class>