问题:
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:
orderItem.Warehouse = warehouse;
orderItem.Order = order;
session.Save(orderItem);
session.Flush();
NHib SQL:
@p0 = ‘0’
@p1 = ‘1’
@p2 = ‘1’
The following code throws exception:
orderItem.Warehouse = warehouse;
orderItem.Order = order;
session.SaveOrUpdate(orderItem);
session.Flush();
NHib SQL:
@p0 = ‘0’
@p1 = ‘1’
@p2 = ‘1’
Exception:
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
回答:
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