-->
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.  [ 4 posts ] 
Author Message
 Post subject: SaveOrUpdate() VS. Save() of <composite-id> object
PostPosted: Sat Feb 04, 2006 4:04 am 
Newbie

Joined: Wed Feb 01, 2006 6:17 am
Posts: 11
In addition to my previous post http://forum.hibernate.org/viewtopic.php?t=955025

I've found unexplained behaviour between session.Save() call and session.SaveOrUpdate call for <composite-id> objects:

Here is the relevant code:

The following code works fine:
Code:
OrderItem orderItem = new OrderItem();
orderItem.Warehouse = warehouse;
orderItem.Order = order;
session.Save(orderItem);
session.Flush();


NHib SQL:
Code:
NHibernate: INSERT INTO dbo.tOrderItem (Price, OrderID, WarehouseID) VALUES (@p0, @p1, @p2)
@p0 = '0'
@p1 = '1'
@p2 = '1'



The following code throws exception:
Code:
OrderItem orderItem = new OrderItem();
orderItem.Warehouse = warehouse;
orderItem.Order = order;
session.SaveOrUpdate(orderItem);
session.Flush();


NHib SQL:
Code:
NHibernate: UPDATE dbo.tOrderItem SET Price = @p0 WHERE OrderID = @p1 AND WarehouseID = @p2
@p0 = '0'
@p1 = '1'
@p2 = '1'


Exception:
Quote:
SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(Int32 expectedRowCount)
at NHibernate.Persister.EntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Boolean[] includeProperty, Object oldVersion, Object obj, SqlString sqlUpdateString, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Object[] oldFields, Object oldVersion, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledUpdate.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
at NHibernate.Impl.SessionImpl.Flush()


The only differnce is the usage of session.SaveOrUpdate instead of session.Save, and the exception occurs because of NHib tries to update a non existing orderItem instead of inserting new one (because no orderItem exists in the DB with the relevant PK (Warehouse + Order)).

Any ideas why NHib tries to update a non existing orderItem instead of inserting new one?

Ruby


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 04, 2006 10:00 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
SaveOrUpdate does not work for composite ids.


Top
 Profile  
 
 Post subject: SaveOrUpdate does not work for composite ids
PostPosted: Thu Mar 02, 2006 11:39 am 
Newbie

Joined: Thu Mar 02, 2006 11:30 am
Posts: 1
This means method Save() cannot be used in a Remoting.


Top
 Profile  
 
 Post subject: SaveOrUpdate and Composite-Id
PostPosted: Fri Mar 03, 2006 12:45 am 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
According to documentation you can get SaveOrUpdate to work with composite ID's.

For this you would have to implement the IsUnsaved method of the interceptor. This method on the interceptor is called when a transient entity is passed to SaveOrUpdate. Inspect the entity (passed as a parameter to the method) and return true to invoke Save or false to invoke Update.

There is another way as well. If you have a Version (or a Timestamp) property for the entity (with the Composite ID) and you do not implement the IsUnsaved method NHibernate will inspect the unsaved-value of the Version(or the timestamp) property to decide whether to invoke Save or Update when you call SaveOrUpdate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.