-->
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.  [ 5 posts ] 
Author Message
 Post subject: how does hibernate implement lazy init of collection?
PostPosted: Thu Apr 06, 2006 1:43 am 
Newbie

Joined: Fri Oct 28, 2005 10:32 pm
Posts: 13
I am curious about the implementation of hibernate about
the lazy initialization of collections.

within a hibernate session,
in which way does hibernate knows some code is using the collection
and then initialize the specified collection?

how?somebody who read this part of
source code please help me explain this,thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 3:37 am 
Newbie

Joined: Thu Nov 17, 2005 12:59 pm
Posts: 18
I guess that the Hibernate create proxies insteard the actual collection elements. An proxy object is called while access to the the collection element. I hope it is clear.

And what will happens, if you call collection.size() - I don't really know.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 09, 2006 9:43 pm 
Newbie

Joined: Fri Oct 28, 2005 10:32 pm
Posts: 13
but it doesnot act like a proxy thing....

when I called

Code:
myObj.getAssociations();


the association is not initilized;

when I called

Code:
logger.debug(myObj.getAssociations());


this time the association is initilized.


does it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 3:24 am 
Newbie

Joined: Thu Nov 17, 2005 12:59 pm
Posts: 18
Quote:
when I called

myObj.getAssociations();


the association is not initilized;


As I see, you don't use real object methods in this case.

opposite in this case:
Quote:
logger.debug(myObj.getAssociations());

you implict call .toString.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 4:09 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Hibernate has it's own custom implementations of each of the collection interfaces. It replaces your collections with an instance of one of these when persist() is called. See here: http://www.hibernate.org/hib_docs/v3/re ... tions.html for details.

This allows hibernate to set up the collections on an object without initialising them. (Which explains why myObj.getAssociations() does not need to initialise the collection). When size() or one of the get methods is called hibernate can issue a query that gets a list of keys of objects in the collection. It can use this query to work out the size of the collection (the result count) and provide proxies for all of the objects in the collection. This is why your second example initialises the collection and one of the proxies.


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