Code section # 1 (save warehouse, save order, save OrderItem):
Code:
ISession session = sessionFactory.OpenSession();
Warehouse warehouse = new Warehouse();
warehouse.Name = "TestWarehouse";
session.Save(warehouse);
Order order = new Order();
order.Price = 100.0;
session.Save(order);
OrderItem orderItem = new OrderItem();
orderItem.Warehouse = warehouse;
orderItem.Order = order;
session.Save(orderItem);
session.Close();
NHib Generated SQLs for code section # 1:NHibernate: INSERT INTO dbo.tWarehouse (Name) VALUES (@p0); select SCOPE_IDENTITY()
@p0 = 'TestWarehouse'
NHibernate: INSERT INTO dbo.tOrder (Price) VALUES (@p0); select SCOPE_IDENTITY()
@p0 = '100'
Code section # 2 (save warehouse, batch save order + OrderItem with setting orderItem.Order = order):Code:
ISession session = sessionFactory.OpenSession();
Warehouse warehouse = new Warehouse();
warehouse.Name = "TestWarehouse";
session.Save(warehouse);
Order order = new Order();
order.Price = 100.0;
OrderItem orderItem = new OrderItem();
orderItem.Warehouse = warehouse;
orderItem.Order = order;
order.OrderItems.Add(orderItem);
ITransaction transaction = session.BeginTransaction();
session.Save(order);
transaction.Commit();
session.Close();
NHib Generated SQLs for code section # 2:NHibernate: INSERT INTO dbo.tWarehouse (Name) VALUES (@p0); select SCOPE_IDENTITY()
@p0 = 'TestWarehouse'
NHibernate: INSERT INTO dbo.tOrder (Price) VALUES (@p0); select SCOPE_IDENTITY()
@p0 = '100'
NHibernate: UPDATE dbo.tOrderItem SET Price = @p0 WHERE OrderID = @p1 AND WarehouseID = @p2
@p0 = '0'
@p1 = '1'
@p2 = '1'
Exception code section # 2: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()
at NHibernate.Transaction.AdoTransaction.Commit()
Code section # 3 (save warehouse, batch save order + OrderItem without setting orderItem.Order = order):Code:
ISession session = sessionFactory.OpenSession();
Warehouse warehouse = new Warehouse();
warehouse.Name = "TestWarehouse";
session.Save(warehouse);
Order order = new Order();
order.Price = 100.0;
OrderItem orderItem = new OrderItem();
orderItem.Warehouse = warehouse;
//orderItem.Order = order;
order.OrderItems.Add(orderItem);
ITransaction transaction = session.BeginTransaction();
session.Save(order);
transaction.Commit();
session.Close();
NHib Generated SQLs for code section # 3:NHibernate: INSERT INTO dbo.tWarehouse (Name) VALUES (@p0); select SCOPE_IDENTITY()
@p0 = 'TestWarehouse'
NHibernate: INSERT INTO dbo.tOrder (Price) VALUES (@p0); select SCOPE_IDENTITY()
@p0 = '100'
NHibernate: UPDATE dbo.tOrderItem SET Price = @p0 WHERE OrderID = @p1 AND WarehouseID = @p2
@p0 = '0'
@p1 = ''
@p2 = '1'
Exception code section # 3: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()
at NHibernate.Transaction.AdoTransaction.Commit()
in code section 1 - no exception but the relation record (orderItem) is not saved
in code section 2 setting orderItem.Order = order cause changes in NHib's SQL (@p1 = '1'). SQLs look fine IMHO (but the exception occurs).
in code section 3 no setting orderItem.Order = order. Relevant NHib's SQL (@p1 = '').
in both code section 2 + 3 - exception occurs.
Is it because of the default implementation (in class OrderItem) of
public override bool Equals(object obj)
public override int GetHashCode()?
If yes, How should I implement it?
Thanks
Ruby