Hi Sergey,
Thanks for the help !
Still implicit version check @my end doesn't work.
Scenario:
1. Get the same Patient record in two Patient Objects( les's say Pat A & B)
2. Do some modification on Pat A and update it.
3. Now Pat B contains stale data. Do some modification on Pat B and try to update it. Update should fail but it works :(
optimistic-lock="all" and version column is added in Patient.hbm.xml file.
Is there any setting needs to be done in order to perform implicit version check?
Sample code is given as below:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MyApp.BusinessEntities.Patient,MyApp.BusinessEntities" table="Patient" lazy="true" dynamic-update="true" optimistic-lock="all">
<id name="Id" column="ID" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<version column="TimeStamp" name="TimeStamp"/>
<bag name="PrescriptionList" cascade="all" inverse="true">
<key column="PatientID" />
<one-to-many class="MyApp.BusinessEntities.Prescription,MyApp.BusinessEntities" />
</bag>
<property column="FirstName" type="String" name="FirstName" length="50" />
<property column="MiddleName" type="String" name="MiddleName" length="50" />
<property column="LastName" type="String" name="LastName" not-null="true" length="50" />
<property column="DOB" type="System.DateTime" name="Dob" />
<property column="Gender" type="MyApp.BusinessEntities.GenderEnumType,MyApp.BusinessEntities" name="Gender" not-null="false" length="1" />
<property column="SSN" type="String" name="Ssn" length="11" />
<many-to-one name="PrimaryCarePhysicianid" column="PrimaryCarePhysicianID" class="MyApp.BusinessEntities.Doctor,MyApp.BusinessEntities" />
<many-to-one name="HomeAddressid" column="HomeAddressID" class="MyApp.BusinessEntities.Address,MyApp.BusinessEntities" />
<property column="HomePhone" type="String" name="HomePhone" length="50" />
<many-to-one name="WorkAddressid" column="WorkAddressID" class="MyApp.BusinessEntities.Address,MyApp.BusinessEntities" />
<property column="WorkPhone" type="String" name="WorkPhone" length="50" />
</class>
<sql-query name="PrescriptionList">
<load-collection alias="prescription" role ="MyApp.BusinessEntities.Patient.PrescriptionList"/>
exec PrescriptionList ?
</sql-query>
</hibernate-mapping>
Code:
//Calling Function
private void button4_Click(object sender, EventArgs e)
{
MyApp.DataAccess.DbOperations crudOpern = new DbOperations();
Patient patientObj11 = (Patient)crudOpern.Read(1, typeof(Patient));
Patient patientObj21 = (Patient)crudOpern.Read(1, typeof(Patient));
patientObj11.LastName = "Update1";
crudOpern.Update(patientObj11);
patientObj21.LastName = "Update2";
crudOpern.Update(patientObj21);
}
//Called Funcion
public object Read(object dataObj, Type objType)
{
try
{
session = NHibernateSessionFactory.OpenSession();
object obj = session.Get(objType, dataObj);
session.Close();
return obj;
}
catch (Exception)
{
if (session.IsOpen)
session.Close();
return null;
}
}
public bool Update(object obj)
{
try
{
session = NHibernateSessionFactory.OpenSession();
ITransaction transaction = session.BeginTransaction();
session.SaveOrUpdate(obj);
session.Flush();
transaction.Commit();
session.Close();
return true;
}
catch (Exception expn)
{
if (session.IsOpen)
session.Close();
return false;
}
}
Thanks,
Deepak