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.  [ 6 posts ] 
Author Message
 Post subject: How to dehydrate collection
PostPosted: Wed Sep 10, 2008 6:07 pm 
Newbie

Joined: Wed Sep 10, 2008 5:59 pm
Posts: 8
We use a collection of images stored on the database on the one of the objects in the application. The main object (appraisal) is "alive" during all the application, when the user navigates to the form where the images are shown we initialize the images collections before doing any data binding. We would like to be able to dehydrate this collections when the user navigates away as this is a pretty heavy collection and the performance downgrade is noticeable.

I'm fairly new to NHibernate and any help/comment is more than welcome.






Hibernate version: 2.2

Mapping documents:
<class name="DomainModel.Appraisal, DomainModel" table="Appraisal">
<id name="Id" column="Id" type="Int32" access="nosetter.camelcase-underscore" unsaved-value="-1">
<generator class="identity"/>
</id>


...


<set name="Images" table="Image" lazy="true" access="nosetter.camelcase-underscore" inverse="true" cascade="all-delete-orphan" order-by="PrintOrder">
<key column="Id"/>
<one-to-many class="DomainModel.Image, Chubb.Appraisal.DomainModel" />
</set>




Code between sessionFactory.openSession() and session.close():
NHibernateUtil.Initialize(appraisal);
NHibernateUtil.Initialize(appraisal.Images);


Full stack trace of any exception that occurs:

Name and version of the database you are using:
SQL Server 2005 9.0.3224

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?
No
Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 12:56 pm 
Newbie

Joined: Tue Sep 09, 2008 12:48 pm
Posts: 6
If I'm understanding what you're asking, you've got an object that contains a collection of images, and that collection is initialized only when required.

The parent object is "alive" all the time? Are you running a web application or a forms based application?

Either way, you should be able to call
session.Evict(object obj);

That should release the object from the first level cache (Your session). At that point, you should have a clean collection.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 1:03 pm 
Newbie

Joined: Wed Sep 10, 2008 5:59 pm
Posts: 8
It's a windows forms application. I will try to evict the collection then and post my results back.
Yes, the parent object stays "alive" through out the life of the application.

It was my understanding that evicting did not mark the object for GC, it only made the object transient (non-persistent)...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 1:40 pm 
Newbie

Joined: Tue Sep 09, 2008 12:48 pm
Posts: 6
After testing, it appears that you're correct, evict only marks it as transient. Because of the way NHibernate proxies the classes and collections, I believe your only alternative would be to reload the parent object from the NHibernate Session when they navigate away.

Or...You might be able to to load two different parent objects from the session. Like so:

ParentType A = session.Load<ParentType>(id);

//Stuff

session.flush(); //persist any changes to A
ParentType B = session.Load<ParentType>(id);
B.ImageCollection.Stuff();
session.Evict(B);
B = null;


It's a huge hack, but other than that, I have no clue how to mark a collection to be re-lazy loaded.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 2:35 pm 
Newbie

Joined: Wed Sep 10, 2008 5:59 pm
Posts: 8
Thanks for the reply.
I'm playing with evicting the collection then clearing it before the end user navigates away. I initialize this collection any time the user opens this form. Let's see how it goes, I'll post the results back.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 1:03 pm 
Newbie

Joined: Wed Sep 10, 2008 5:59 pm
Posts: 8
I'm using the following code:


session = NHibernateHelper.GetSession(NHibernateHelper.SessionKey.MyView);
session.Evict(parentObj.MyCollection); //check if this unloads the collection
parentObj.MyCollection.Clear();


However later on when I try to save or update the parentObj I see Nhibernate tries to do the deletions for parentObj.MyCollection


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.