-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: incorrect collection data in a new session
PostPosted: Fri Oct 07, 2005 11:52 am 
Beginner
Beginner

Joined: Wed Jun 29, 2005 10:40 am
Posts: 30
Location: denver, co
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>


Top
 Profile  
 
 Post subject: Update: Evicting doesn't work either
PostPosted: Fri Oct 07, 2005 11:57 am 
Beginner
Beginner

Joined: Wed Jun 29, 2005 10:40 am
Posts: 30
Location: denver, co
I tried to evict the persisted object from the session and fetch the original, but the collection items problem still exists. I have cascading set to all-delete-orphans, shouldn't the "evict" operation also cascade to the persitent collection?

This is what I tried:
Set FlushMode to NONE
Fetch a persited object
Modify the persisted object, add to child collections, etc, without flushing changes.
Evict the object from the session.
Re-fetch the object from the session.
The result is that all basic properties are freshly fetched, but the child collections are still those of the evicted object!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 12:09 pm 
Beginner
Beginner

Joined: Wed Jun 29, 2005 10:40 am
Posts: 30
Location: denver, co
Never mind - I had a problem in my mapping file. The type within the collection was set to the parent type.

Sorry!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.