2011-11-08 59 views
0

我有一個NHibernate實現和使用Lazy Loading的項目。我在這個項目中有三個班級:個人,身份和家庭。意思是一個人有一個身份和一個家庭名單。映射是:在NHibernate的併發實現

<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> 

我的問題是併發性討論。第一個問題是: 更新Person時,Person表中的版本字段將更新且爲true,但是 更新Identity時,Person表中的版本字段不會更新並且爲false。 爲什麼在Identity更新時,數據庫中Person表中的版本字段沒有更新?

第二個問題是: 當加入一個家庭,在Person表版本字段將更新,是真實的, 當一個家庭被刪除,在Person表版本字段將更新,是真實的,但 當系列更新時,人員表格中的版本字段不會更新並且是錯誤的。 爲什麼當Family更新時,數據庫中Person表中的版本字段沒有更新?

回答

1

只有當實體本身的屬性/集合更新不依賴實體時才更新版本字段,因此在添加/刪除系列時更改了人員集合,更新家庭並不會更改人員。如果你的語義,一個人不應該當一個家庭被改變,然後更新它包裝在一個交易

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

更新人的版本時,身份的變化,你可以在Person.hbm.xml

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

值得注意的是,家人總是被人更新。 獨自一家不更新。我應該怎麼做,那個人總是更新? – Ehsan

+0

'family.Person.Version = family.Person.Version + 1;'在每個屬性可能。我得考慮一下 – Firo

+0

謝謝。我的問題通過這個技巧解決了。當然新版本的價值並不重要,只是人中一個領域的重要變化。 – Ehsan