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