NHibernate 0.9
SQL Server 2k
Within my app, I have the need to make modifications to a persisted object and then compare it to the original object as it was before modification. This works with simple properties, but with collections, unpersisted additions and changes.
Basically my process is this:
Set FlushMode to NONE
In Session 1, fetch a persisted object.
Modify that persisted object without saving it.
Open a new session and use it to retrieve the original persisted object for comparison.
The problem is that collection items added (but not saved) in session 1 are visible in session 2. I thought that sessions would be completely independent of one another, fetching the "real" data from the database if changes in another session had not yet been flushed.
This is a pretty common scenario for me. Is there any way I can fetch the original version of an object graph to compare with a modified graph?
Here is my code from a small unit test that showed this:
Code:
//create a new instance and save it (with no collection items)
SimpleType t1 = new SimpleType();
t1.Name = "TestType";
t1.Description = "TestTypeDesc";
SimpleTypeRepository.Current.SaveOrUpdate(t1);
//change properties WITHOUT FLUSHING THE CHANGES.
t1.Name = "CHANGED!";
//Create a new collection item and add it to the persisted object WITHOUT FLUSHING.
SimpleCollectionItem item = new SimpleCollectionItem();
item.Name = "CollItem";
item.SimpleType = t1;
t1.SimpleCollectionItems.Add(item);
//fetch the same object in a new session.
SimpleType t2 = SimpleTypeRepository.Current.GetByIdInNewSession(t1.Id);
Assert.IsFalse(t1 == t2); //passes
Assert.IsFalse(t1.SimpleCollectionItems == t2.SimpleCollectionItems); //passes
Assert.AreEqual("TestType", t2.Name); //passes
Assert.AreEqual(0, t2.SimpleCollectionItems.Count); //FAILS
Here are my mappings:
The SimpleType class:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="ScratchPad.SimpleType, ScratchPad.DomainModel" table="ScratchPad.dbo.SimpleType">
<id name="Id" type="Int32" unsaved-value="0" column="SimpleTypeId">
<generator class="native" />
</id>
<property name="Name" column="`Name`" not-null="true"/>
<property name="Description" column="`Description`" not-null="false"/>
<bag name="SimpleCollectionItems" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="SimpleTypeId"/>
<one-to-many class="ScratchPad.DomainModel.SimpleType, ScratchPad.DomainModel"/>
</bag>
</class>
</hibernate-mapping>
The SimpleTypeCollectionItem class:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="ScratchPad.DomainModel.SimpleCollectionItem, ScratchPad.DomainModel" table="ScratchPad.dbo.SimpleCollectionItem">
<id name="Id" type="Int32" unsaved-value="0" column="SimpleCollectionItemId">
<generator class="native" />
</id>
<property name="Name" column="`Name`" not-null="true"/>
<many-to-one name="SimpleType" class="ScratchPad.DomainModel.SimpleType, ScratchPad.DomainModel" column="SimpleTypeId" not-null="true"/>
</class>
</hibernate-mapping>