Hibernate SaveOrUpdate() VS. Save() of <composite-id> object

问题:

 

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

回答:

 

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.



引文来源  Hibernate Community ? View topic – SaveOrUpdate() VS. Save() of object