NHibernate flush should save only dirty objects
Posted
by Emilian
on Stack Overflow
See other posts from Stack Overflow
or by Emilian
Published on 2010-03-17T14:00:54Z
Indexed on
2010/03/17
14:01 UTC
Read the original article
Hit count: 397
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();
© Stack Overflow or respective owner