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? If I don't add order lines to first
order then everything works as expected.

// 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();

Here is the mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  assembly="Teste" namespace="Teste.Core">
    <class name="Order" table="Orders" optimistic-lock="version"
         dynamic-update="true" dynamic-insert="true">
        <id name="id" column="Id" access="field" type="Guid">
            <generator class="guid.comb"/>
        </id>
        <version name="version" column="Version" access="field"
type="Int64" />
        <property name="approved" column="Approved" access="field"
              type="Boolean" not-null="true"/>
        <set name="orderLines" table="OrderLines" access="field"
lazy="true"
         optimistic-lock="true">
            <key column="OrderId" foreign-key="fk_Order_OrderLine"/>
            <composite-element class="OrderLine">
                <property name="productName" column="ProductName"
access="field"
                  type="String"/>
                <property name="productPrice" column="ProductPrice"
access="field"
                  type="Decimal"/>
                <property name="quantity" column="Quantity"
access="field"
                  type="Int32"/>
            </composite-element>
        </set>
    </class>
</hibernate-mapping>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to