I have a user table as follows
Code:
create table usr
(
UsrId int,
UsrCode varchar(8),
UpdatedBy int
)
with data
UsrId UsrCode UpdatedBy
----------- ---------- -----------
1 tusr1 1
2 tusr2 1
C# code
public class Usr
{
protected int _usrId;
protected string _usrCode;
protected Usr _updatedBy;
public virtual int Id
{
get {return _id;}
set {_id = value;}
}
public virtual string UsrCode
{
get { return _usrCode; }
set { _usrCode = value; }
}
public virtual Usr UpdatedBy
{
get { return _updatedBy; }
set { _updatedBy = value; }
}
}
Nhibernate.hbm
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Usr, Test" table="Usr">
<id name="Id" type="Int32" unsaved-value="null">
<column name="UsrId" length="4" sql-type="int" not-null="true" unique="true" index="PK_Usr"/>
<generator class="increment"/>
</id>
<property name="UsrCode" type="String">
<column name="UsrCode" length="10" sql-type="varchar" not-null="true"/>
</property>
<many-to-one name="UpdatedBy" class="Test.Usr, Test" lazy="false">
<column name="UpdatedBy" length="4" sql-type="int" not-null="false"/>
</many-to-one>
</class>
</hibernate-mapping>
Problem when I am modifying RequestedBy, the main class is also update. For example
Usr usr = session.Get<Usr>(1);
usr.UpdatedBy.Id = 2; <===== At this line, usr.Id is also updated to 2. It looks usr and UpdatedBy is pointing to the same Usr object (due to NHibernate caching?).
How can I tell NHibernate not to use cache? I've tried Evict(typeof(Usr)) after Get but it's too late. Should I explore IInterceptor? If yes any hint?
Thanks.