-->
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.  [ 10 posts ] 
Author Message
 Post subject: lazy initialization question...
PostPosted: Tue Sep 23, 2003 1:43 pm 
Beginner
Beginner

Joined: Fri Sep 05, 2003 10:17 am
Posts: 42
I have read the documentation and i think i am doing something that is not permitted. I have in my mapping xml file specified to use lazy initialization, i also have a set definition in the mapping file for a one-to-many class.

What i am trying to do in my code it return, or keep in memory after i close the hibernate session, a reference to the Set. I want to be able to pass the Set object within my app so i can process it with business logic. It makes no sense to me to need a hibernate session open for the time i am processing this object. Is this the way lazy init. is suppose to work; need to have an open session to get/modify the data in the Set?

Please folks straighten me out here. lazy to me means don't get the data 'till you need it, then once you have it, you have it and can modify it without needing handles to other objects.

thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 2:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
There are two use cases:
1) You will not require the set of data. Hence lazy loading / proxy will not load the set. It can be sent to the UI and non-proxy data is available.
2) You require the data. Hence you will need to load the data either by getting the set or using hibernate.initialise(). Both require the session to be active as the proxy has a reference to it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 3:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Lazy returns proxies as placeholders for given values. However those proxies are only valid within the boundary of the session that generated them, unless they are accessed/loaded prior to leaving that session boundary.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 3:43 pm 
Beginner
Beginner

Joined: Fri Sep 05, 2003 10:17 am
Posts: 42
thanks for your reply. It appears that i will need a session open in order to get at the Set data. So here is another question ... what if i do something like.
Code:
// copy to a collection
      while( programSet.iterator().hasNext() )
      {
         col.add( programSet.iterator().next() );
      }



i suspect that the client that uses the collection after the session has been closed will get a NullPointerException. am i corrcect? basically copy the contents of the set into a collection while you still have the session open then close the session and return the collection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 5:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Once a collection proxy has been initialized it is initialized. Think of a proxy collection as a big eleborate:
Code:
    private Collection myCollection;
    public Collection getMyCollection()
    {
        if (myCollection == null)
            loadMyCollection();
        return myCollection;
    }
    ...


Rather than iterate, just access the collection in some way to initialize it. This can be explicitly through session.initialize() or something like getting the size of the collection...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 5:33 pm 
Beginner
Beginner

Joined: Fri Sep 05, 2003 10:17 am
Posts: 42
Yes but I always will need an open session to populate the proxy. I think what i will do is add a threadlocal instance variable to the class then open and close the session as need be in the class. The class will implement interface methods to get and close the session. The Set will use
Hibernate.initialize( object proxyObject) to init the proxy with data. Does this seem like a good solution?

thanks again for all your help.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 5:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Then why are you making this lazy again?

I mean if you always need this collection initialized, why even go through all this; just make it non-lazy.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 6:56 pm 
Beginner
Beginner

Joined: Fri Sep 05, 2003 10:17 am
Posts: 42
I am using lazy initialization because we have a fair amount of data (based in collections) that we do not want to load all the time. these collections (Sets) contain entities and according to the hibernate docs i should use lazy initialization. i do notice a latency hit when not using lazy initialization but it is not that bad. i cannot say the data in the collections (Sets) will be required 100% of the time, most likely 80% of the time. If you think that hibernate will perform with lazy init. turned off then perhaps this is the way i should go. basically i was attempting to only initialize the objects when i want them. this is why i thought lazy init. would work. if the threadlocal instance and the getSession and closeSession methods in the objects that have the Set (mapped collections) is the wrong way to manage lazy init. then please let me know what the correct way is.

we do not want to take a performance hit for building collection on every client request.

thanks again and hope i am being clear.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 9:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
If you really want to go this route, then I would suggest having those particualr domain entity classes implement the net.sf.hibernate.Lifecycle interface. Check out its docs http://www.hibernate.org/hib_docs/api/net/sf/hibernate/Lifecycle.html

Depnding on how your architecture is set up, a much better approach is to externalize this lazy collection init somewhere outside the persistent classes themselves through a call to session.initalize() whenever you encounter one of these situations where it needs to be. Where best to do that you would know better, as you know your architecture better than I.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 9:12 pm 
Beginner
Beginner

Joined: Fri Sep 05, 2003 10:17 am
Posts: 42
excellent thank you for your help on this. i will read what is at the other end of the link and implement the changes.

best
-derrick


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