-->
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.  [ 9 posts ] 
Author Message
 Post subject: "dynamic" initialization of collections
PostPosted: Wed Dec 06, 2006 9:31 am 
Beginner
Beginner

Joined: Wed Aug 10, 2005 12:09 am
Posts: 30
Hibernate version: 3.2

Hi, I have an object with about 10 or so collections (set), which are mapped with fetch="select" and lazy="true".

Problem: when I use session.get or sesion.load I want to initialize all the collections exactly as if lazy="false" in the mapping, without having to "hardcode" the property names.

My current understanding is:

obj = session.get(someclass)
Hibernate.initialize(obj.getSet1())
- now iterate through the collection and call initialize on each reference)
Hibernate.initialize(obj.getSet2())
- initialize all set 2 objects
Hibernate.initialize(obj.getSet3())
- initiliaze all set 3 objects
Hibernate.initialize(obj.getSet4())
etc

Q. does the above code initialize as per lazy="false"?

If I add a new relationship to the mapping I have to remember to update this code to initialize the new collection - I think I want a method which does all necessary initialization for me dynamically.

Q. does hibernate have a utility which does this?

if not can anyone suggest a dynamic solution...should I be writing my own utility method which reads the mapping and dynamically initializes using java reflection api...or something?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 11:22 am 
Newbie

Joined: Mon Dec 04, 2006 5:01 pm
Posts: 5
Could I get a little more clarification on why you want to use the Hibernate.initialize(obj.getCollection()) as opposed to setting lazy="false" in the XML? Is it that some situations don't need fully initialized collections, while others do?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 7:16 pm 
Beginner
Beginner

Joined: Wed Aug 10, 2005 12:09 am
Posts: 30
The reason is that when I do a search (criteria query) I dont need to retrieve all the associations, only when a record is selected do I load all.

Do you think (in this case) I should map my relationships as lazy="false" and use a non-entity pojo for my query results?

How do I build a dynamic query using a non-entity class? - it seems the criteria API only allows entity classes, so that leaves me with HQL and needing to build a query string by hand???

Thanks for your help


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 16, 2006 2:20 pm 
Beginner
Beginner

Joined: Sat Dec 16, 2006 1:52 pm
Posts: 40
Did you find an answer as this is exactly what I need as well. I want all or nothing lazy loading, not one at a time as needed. I am not sure if lazy means the collection is populated one at a time as you iterate through, or if the query loads up the whole collection at once!?

Do you really have to initialize the items in the collection one at a time as well? If you have to do that what is the point in initializing the collection as you can just do it lazily since you are about to iterate over the whole collection!?


Top
 Profile  
 
 Post subject: Re: "dynamic" initialization of collections
PostPosted: Sat Dec 16, 2006 4:34 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
shnplr wrote:
Q. does the above code initialize as per lazy="false"?

Er, yes.

shnplr wrote:
Q. does hibernate have a utility which does this?

if not can anyone suggest a dynamic solution...should I be writing my own utility method which reads the mapping and dynamically initializes using java reflection api...or something?

You can access the mapping via sessionFactory.getClassMetadata(YourClass.class) (see also getCollectionMetadata() method). So if you're mapping say Foo.java which owns your numerous collections, retrieve the classMetadata of this class after having loaded the instance of YourClass you want. Then call Hibernate.initialize() by reflection on each collection. I did it recently, it works ok.

You should maybe map your collections using fetch="subselect" so as there's only one select, even if you have many YourClass instances.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 16, 2006 4:42 pm 
Newbie

Joined: Sat Nov 25, 2006 2:56 pm
Posts: 11
1. If your collection is marked lazy=true" then it's my understanding that the collection is lazily initialized. That is, nothing in the collection is initialized until the first element is retrieved from the collection. At that point, everything in the collection is loaded all at once.

2. If you call Hibernate.initialize(), that loads the contents of the entire collection just like if you had lazy="false".

3. I'm not aware of any method to dynamically initialiaze every collection an object has - you could probably write one yourself that uses reflection.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 16, 2006 7:12 pm 
Beginner
Beginner

Joined: Sat Dec 16, 2006 1:52 pm
Posts: 40
PaulRivers wrote:
1. If your collection is marked lazy=true" then it's my understanding that the collection is lazily initialized. That is, nothing in the collection is initialized until the first element is retrieved from the collection. At that point, everything in the collection is loaded all at once.

2. If you call Hibernate.initialize(), that loads the contents of the entire collection just like if you had lazy="false".

3. I'm not aware of any method to dynamically initialiaze every collection an object has - you could probably write one yourself that uses reflection.


This should work for me. The objects within the collection will have lazy=false. So when the object is loaded into the collection I should not need to further initialize it right ?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 14, 2007 3:22 pm 
Beginner
Beginner

Joined: Sat Dec 16, 2006 1:52 pm
Posts: 40
Well no that did not exactly work. Some of the objects in the collection also have collections in them. I need the whole graph to be loaded at once.

It would work better if I could just force one of my topmost collection NOT to load while all the others remain with laze=false. Having trouble with that though :(


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 14, 2007 6:22 pm 
Beginner
Beginner

Joined: Sat Dec 16, 2006 1:52 pm
Posts: 40
OK, all fixed once i made sure I had default access constructor and the class was not final...


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