-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: NHibernate tries to update my primary key
PostPosted: Sat Mar 11, 2006 12:56 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
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>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 1:09 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
Ok, I've got it working.

It was indeed an error in my mapping file.

In my Customer mapping file, I had to change this:

Code:
<bag name="orders" access="field" cascade="save-update" lazy="true">
      <key column="Id"/>
      <one-to-many class="ShopDomain.Order, ShopDomain"/>
</bag>

into this:
Code:
<bag name="orders" access="field" cascade="save-update" lazy="true">
      <key column="CustomerId"/>
      <one-to-many class="ShopDomain.Order, ShopDomain"/>
</bag>


(Note the key column).

However, I am now wondering why NHibernate performs this UPDATE ?
(It is still there, but now the update is on the 'good' column. However, this shouldn't be necessary, should it ?
This is what NHibernate now generates :
First, it inserts my new order and the new orderline:
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  6:05:00: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 = 45.00000, @p2 = 1, @p3 = 7

Then, it does these update queries:
Code:
exec sp_executesql N'UPDATE tblOrder SET CustomerId = @p0 WHERE Id = @p1', N'@p0 int,@p1 int', @p0 = 2, @p1 = 7

exec sp_executesql N'UPDATE tblOrderLine SET OrderId = @p0 WHERE Id = @p1', N'@p0 int,@p1 int', @p0 = 7, @p1 = 5


Is there a way to avoid the generation of these 2 update statmenets ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 7:53 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
The updates have dissapeared with adding an inverse element in my mapping file.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.