简体   繁体   中英

Concurrency implementation in NHibernate

I have a project by NHibernate implementation and using Lazy Loading. I have three class in this project : Person, Identity and Family. Is mean a Person has a one Identity and a list of Family. Mapping is :

<class name="Person" table="Person_Person" >

    <id name="Id" type="Int64" unsaved-value="0">
      <generator class="native" />
    </id>

    <version name="Version" />

    <property name="Name" column="Name"
              type="String(255)" update="true" insert="true" access="property" not-null="false" />

    <one-to-one name="Identity" property-ref="Person"
      class="Domain.Entities.PersonIdentity,Domain.Entities" cascade="delete" fetch="select" />

    <bag name="Families" inverse="true" table="Person_Family" cascade="all-delete-orphan" >
      <key column="Person_id_fk"/>
      <one-to-many class="Domain.Entities.Family,Domain.Entities"/>
    </bag>

</class>

My problems is concurrency discuss. First problem is that : When a Person is updated, version field in Person table Will be updated and is true, But When a Identity is updated, version field in Person table Will not updated and is false. Why when Identity update, version field in Person table in Database not update?

Second problem is that : When a Family is added, version field in Person table Will be updated and is true, and When a Family is deleted, version field in Person table Will be updated and is true, But When a Family is updated, version field in Person table Will not updated and is false. Why when Family update, version field in Person table in Database not update?

the version-field is only updated if properties/collections of the entity itself updates not dependent Entities, so while adding/removing Families alters the collection of Person, updating a Family doesnt change Person at all. if you have the semantic that a Person should not be updated when a family is altered then wrap it in a transaction

using (var tx = session.BeginTransaction())
{
    session.SaveOrUpdate(person);
    tx.Commit(); // throws here if there is conflict in a family
}

to update Version of Person when Identity changes you can in Person.hbm.xml

<join table="PersonIdentityTable" column="PersonId">
  <component name="Identity">
    // map properties here
  </component>
</join>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM