I use Version tag to manage concurrency. When i load a specific person like this (person holds 2 collections, history, address):
ISession session1 = _sessionFac.OpenSession();
ITransaction trans1 = session1.BeginTransaction();
person = session.CreateCriteria(typeof(Person))
.Add(Expression.Eq("Name", xyz))
.SetMaxResults(1)
.List();
trans1.Commit();
session1.Close();
Then i DON'T make any changes and save it:
ISession session2 = _sessionFac.OpenSession();
ITransaction trans2 = session2.BeginTransaction();
session2.SaveorUpdate(person)
session2.Flush();
trans2.Commit();
session2.Close();
Then the column version in all 3 tables are incremented +1. Shouldn't they be incremented, only if something has changed?
2) And if i change/add a new history between the 2 sessions. version column of address is incremented, too. But why? Shouldn't only the version column inperson and address be incremented?
3) If 2 users load the same person, user A changes an address, user B changes a history, user A saves person , user B gets of course a StaleObjectException while saveorupdate(person). I do understand, why StaleObjectException is coming in this case (person version is higher). But i think, when user A saves person, only person and address version column has to be incremented? And user B at least can save history?
Am i misunderstanding something or is something wrong in the mappings? In the DB, type of version is int, not null.
Code:
<class name="Person" table="Person" lazy="false">
<id name="PNr" column="PNr" type="Int32" unsaved-value="null">
<generator class="native"/>
</id>
<version name="Version" column="version" type="Int32" unsaved-value="0" />
<bag name="AddressList" inverse="true" lazy="false" cascade="save-update" >
<key column="PNr" />
<one-to-many class="Address" />
</bag>
<bag name="HistoryList" inverse="true" lazy="false" cascade="save-update" >
<key column="PNr" />
<one-to-many class="History" />
</bag>
<class name="Address" table="Address" dynamic-update="true" dynamic-insert="true" >
<version name="Version" column="version" type="Int32" unsaved-value="0" />
<many-to-one name="PNr" column="PNr" class="Person" update="false"/>
<class name="History" table="History" dynamic-update="true" dynamic-insert="true" >
<version name="Version" column="version" type="Int32" unsaved-value="0" />
<many-to-one name="PNr" column="PNr" class="Person" update="false"/>