These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: NHibernate flush should save only dirty objects
PostPosted: Wed Mar 17, 2010 12:05 pm 
Newbie

Joined: Wed Mar 17, 2010 11:41 am
Posts: 1
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.

Code:
// 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:

Code:
<?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>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.