tags:

views:

47

answers:

1

Hi

I have just started working with NHibernate. Have found it pretty good till now but have hit an issue that I am unable to solve. Any help will be greatly appreciated.

I am trying to use the "version" feature to help implement optmistic locking. Below is my mapping file and a portion of code from the related class. My problem is that no matter how many times I modify any field, the "Version" column on the database does not get updated.

My mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="MyTest"
                   assembly ="MyTest">
  <class name="Project" table="dbo.M_Project">
    <id name="ProjectId" type="Int32" unsaved-value="null">
      <column name="ProjectId" sql-type="int" not-null="true" unique="true" />
      <generator class="native" />
    </id>
    <version name="Version" column="Version"/>
    <property name="ProjectName" type="String">
      <column name="ProjectName" length="100" sql-type="nvarchar" not-null="true"/>
    </property>
    <property name="Updated" type="bool">
      <column name="Updated" sql-type="bit" not-null="true"/>
    </property>
    <property name="NoOfItemsWithExceptions" type="int">
      <column name="NoOfItemsWithExc" sql-type="int" not-null="true"/>
    </property>
    <property name="MenuState" type="int">
      <column name="MenuState" sql-type="int" not-null="true"/>
    </property>
    <property name="Initialized" type="bool">
      <column name="Initialized" sql-type="bit" not-null="true"/>
    </property>
    <property name="InventoryRun" type="bool">
      <column name="InventoryRun" sql-type="bit" not-null="true"/>
    </property>
    <property name="ProductionForecastRun" type="bool">
      <column name="ProductionForecastRun" sql-type="bit" not-null="true"/>
    </property>
    <property name="MonthlyUpdate" type="bool">
      <column name="MonthlyUpdate" sql-type="bit" not-null="true"/>
    </property>
    <bag name="SKUList" table="dbo.P_SKU" inverse="true" lazy="true" cascade="save-update">
      <key column="ProjectId" />
      <one-to-many
        class="SKU"/>
    </bag>
  </class>
</hibernate-mapping>

The associated version property:

    private int _version;
    public virtual int Version
    {
        get { return _version; }
        set { _version = value; }
    }

Thanks!

A: 

It might be necessay to explicity set the optimistic-lock attribute (in theory it's not). For instance

<class name="Project" table="dbo.M_Project" optimistic-lock="dirty">

Here you have more info about it

Claudio Redi
My understanding is that the optimistic-lock property should be "version"(the default) if the intention is to have optimistic locking over all the fields. However I did try "dirty" but it didn't work.
RHK