NHibernate flush should save only dirty objects
- by Emilian
Why NHibernate fires an update on firstOrder when saving secondOrder in the code below? I'm using optimistic locking on Order. Is there a way to tell NHibernate to update firstOrder when saving secondOrder only if firstOrder was modified?
// Configure
var cfg = new Configuration();
var configFile = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"NHibernate.MySQL.config");
cfg.Configure(configFile);
// Create session factory
var sessionFactory = cfg.BuildSessionFactory();
// Create session
var session = sessionFactory.OpenSession();
// Set session to flush on transaction commit
session.FlushMode = FlushMode.Commit;
// Create first order
var firstOrder = new Order();
var firstOrder_OrderLine = new OrderLine
{
ProductName = "Bicycle",
ProductPrice = 120.00M,
Quantity = 1
};
firstOrder.Add(firstOrder_OrderLine);
// Save first order
using (var tx = session.BeginTransaction())
{
try
{
session.Save(firstOrder);
tx.Commit();
}
catch
{
tx.Rollback();
}
}
// Create second order
var secondOrder = new Order();
var secondOrder_OrderLine = new OrderLine
{
ProductName = "Hat",
ProductPrice = 12.00M,
Quantity = 1
};
secondOrder.Add(secondOrder_OrderLine);
// Save second order
using (var tx = session.BeginTransaction())
{
try
{
session.Save(secondOrder);
tx.Commit();
}
catch
{
tx.Rollback();
}
}
session.Close();
sessionFactory.Close();