-->
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.  [ 11 posts ] 
Author Message
 Post subject: Problem while loading an object with a lazy loading set
PostPosted: Fri Dec 05, 2003 1:00 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Hi,

I use a persistent object A which has a set declared with lazy initialization.
It works perfectly usually, but there is one bad case:

* i create a new object A (new) without setting the Set values
* i save it
* i forward to another servlet action
* this action loads the objet
* the returned object is exactly the one i saved, but the Set of objects is "null", so I can't iterate on it and trig the lazy loading operations

What did I miss ?

Yann


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 1:34 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Are you sure you are not retrieving Set from the object you just created? Because objects loaded by Hibernate from database have non-null Sets even if these Sets are lazy.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 1:39 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
In the doLoad method of SessionImpl, there is :
Code:
      // LOOK FOR LOADED OBJECT
      Object old = getEntity(key);
      if (old!=null) {  //if this object was already loaded
         Status status = getEntry(old).status;
         if ( checkDeleted && ( status==DELETED || status==GONE ) ) {
            throw new ObjectDeletedException("The object with that id was deleted", id);
         }
         lock(old, lockMode);
         if ( log.isTraceEnabled() ) log.trace( "resolved object in session cache " + MessageHelper.infoString(persister, id) );
         return old;
      }



And I get a non-null old object. It seems Hibernate is keeping a reference on my newly saved object but it resend it without setting the Set which need to be lazy-loaded !!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 1:44 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Ah... It may be possible if you save object and load it within the same session. Since I have never done that way (usually object is always loaded from another session), I have never expirienced this.

But what lazy collection initialization are you talking about then? :) If collection is null on newly created object, there is nothing to initialize. Can't you just add null check before iteration?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 2:02 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
... or implement your Set getters as:

Code:
public Set getClients()
{
    if (clients == null)
        clients = new HashSet();
    return clients;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 8:50 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
This would be a way, but I doubt Hibernate simply instantiate a HashSet to perform lazy loading operations. I suppose it is a very special Set which manages the initialization.

I would like to have the opinion of the Hibernate team about my problem...

And I can give more details if needed.

Regards,
Yann


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 8:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I personally think that dimas suggestion is perfect. Hibernate wraps any HashSet automatically at flush-time.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 9:26 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
I tested it, but, as I thought, the used Set is the one I have initialized (new HashSet() )...and it doesn't perform any lazy operations....so I still don't get my data in the set.

:-(

Isn't there a problem in the temp cache of objects ?
When Hibernate temporarily stores in its session the object, couldn't it set the lazy-loadable Sets to a state "NOT_YET_LOADED" or something like that ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 9:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Oh, I understand what you are trying to do.

Don't do this.

Use an object obtained from session.get() or session.load(), never one instantiated with new, if you want to represent an existing row..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 9:30 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
What do you expect will Hibernate lazy-load if you JUST CREATED the object?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 10:26 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
You're both right. I misunderstood the developer's problem. It was not a newly created object but an object already persisted in the DB...
So, I asked him to do a session.load() instead of NEW Object, and it works !

Thank you dimas and gavin !


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