-->
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.  [ 4 posts ] 
Author Message
 Post subject: Lazy Load Manually
PostPosted: Mon Dec 19, 2005 3:27 pm 
Newbie

Joined: Mon Dec 19, 2005 2:54 pm
Posts: 2
So I am new to nhiberate and have been reading the documentation about lazy loading trying to find a work around for lazy loading after a session is closed.

I found the Lazy Loading topics

Which mentioned using session.lock and initialize

And so far as I have tested this seems to be working

I have a static convenience method that takes a persistant entity (IDataItem is my own protocol so don't worry about it) and an IList
Code:
      public static IList ReattachLazyCollection(IDataItem anEntity,IList aCollection)
      {
         PersistentCollection tCollection =aCollection as PersistentCollection;
         
         if (tCollection != null && !NHibernateUtil.IsInitialized(tCollection)) {
            using (ISession tSession = Factory.OpenSession()) {
               tSession.Lock(anEntity,LockMode.None);
               NHibernateUtil.Initialize(tCollection);
            }
         }
         
         return aCollection;
      }


And it's called in my lazy IList getters such as

Code:
      [NHMA.Bag(0,Lazy=true)]
      [NHMA.Key(1,Column="PLAN_ROW_ID")]
      [NHMA.OneToMany(2,ClassType=typeof(Participant))]
      public IList Participants
      {
         get {
            return Database.ReattachLazyCollection(this,_participants);
         }
      }


And since i'm using the field.camelcase-underscore property access, this doesn't seem to cause any trouble. So my question is for those who have an idea of what is going on underneath in Nhibernate, are there any downsides to doing this? Am I using ISession.Lock correctly?

Thanks, Jay


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 10:57 am 
It looks like to me that will work. It's what I use since I do not want to have my callers reinitialize collections, thus breaking abstraction.

What I would really like to do is use IInterceptor and put in some sort of proxy collection that does delayed loading for me. However, I could not get this to work, as when this collection does its "automatic" reattachment of the session, it fails because NHibernate is looking for PersistentCollections when you reattach.

I continue to hope someday that NHibernate will either implement some form of built-in delayed loading, or better yet have a IReconnectSession interceptor interface so the user can do it.


Top
  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 11:54 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
The problem I can see with this approach is that Lock will throw an exception if the object passed to it has been modified since it was loaded. In that case you'd need to call Update or SaveOrUpdate to reattach the object to the session (which would, of course, cause the object to be updated to the underlying datastore too).

Just my 2c worth...

Symon.


Top
 Profile  
 
 Post subject: ah
PostPosted: Wed Dec 21, 2005 12:32 pm 
Newbie

Joined: Mon Dec 19, 2005 2:54 pm
Posts: 2
s.rottem wrote:
The problem I can see with this approach is that Lock will throw an exception if the object passed to it has been modified since it was loaded. In that case you'd need to call Update or SaveOrUpdate to reattach the object to the session (which would, of course, cause the object to be updated to the underlying datastore too).

Just my 2c worth...

Symon.


Thanks Symon, that's the kind of gotcha I was worried about, and useful to know. 90% of the time I'm using these objects with lazy relations in mass groups, and at those times i don't need the relations and these objects are pretty much read only, it's only when i'm using the objects individually that I need the relations and read/write functionallity. Knowing this, I may switch the intialization trigger to when set is called on a property. I'll probably wait till I get farther in my project and look at my acutal usage though.


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