-->
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.  [ 8 posts ] 
Author Message
 Post subject: Manual fetching of collections
PostPosted: Thu Mar 05, 2009 5:47 pm 
Newbie

Joined: Wed Mar 04, 2009 6:36 pm
Posts: 8
Hi all,

While Hibernate supports fetching strategies like batch fetching and subselect fetching which allow to more efficiently load a specific collection for a whole set of root objects, I wanted to ask whether it is also possible to load a collection (or other property) for a user-defined set of root objects.

I'm asking since I'm trying to optimize a performance critical part of an application where I'm loading multiple collections of a whole set of root objects and the batch/subselect fetching strategies do not always optimally load the collections on the specific root objects I want.

More generally, I was wondering what would be the most efficient way to prefetch multiple collections on multiple root objects in Hibernate?

I could think of having a method like:

Code:
Hibernate.initializeProperty(Class entityClass, Serializable[] ids, String propertyName);


This would allow to load the given property from a set of objects of the given entity type. Is something like this possible (maybe in some other way?)?

I hope my explanation is clear enough since I'm rather new to Hibernate.

Thanks in advance for any help!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 5:00 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
May be you can use a HQL like:

Code:
"select propertyName from EntityClass where id in (1,2,3,4)"

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 10:46 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
You could add fetch="subselect" in the collection mapping.

If you retrieve your root objects via HQL and access the collection on the first object, all collections for all root objects will be loaded automatically.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 8:26 pm 
Newbie

Joined: Wed Mar 04, 2009 6:36 pm
Posts: 8
Hi Litty,

first of all, thanks for your response!

littypreethkr wrote:
May be you can use a HQL like:

Code:
"select propertyName from EntityClass where id in (1,2,3,4)"

This is indeed what I want to achieve but if I execute this query and subsequently I access the loaded property on any of the root objects specified in the query, Hibernate nevertheless triggers another query in order to load the same collection.

Somehow, it seems that Hibernate does not associate the loaded collections with their respective root objects. Or am I doing something wrong? I'm using version 3.2.6.GA of Hibernate.

What actually seems to work is a HQL query like the following:
Code:
from EntityClass e left join fetch e.propertyName where e.id in (1,2,3,4)

However, the problem here is that the query always returns the root object as well (for every entry of the collection) which I want to avoid since I already have the root objects loaded and I want to load multiple collections for all the root objects so this way I would retrieve lots of redundant data from the database.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 4:14 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
as i wrote, use fetch=subselect in the mapping and just access the first collection, it's probably exactly what you want


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 8:05 am 
Newbie

Joined: Wed Mar 04, 2009 6:36 pm
Posts: 8
Hi pkleindl,

I have played around a bit with the subselect fetching strategy and it would actually do what I want. What I need to check is whether it would work fine in our application where we typically have the following pattern:

1. load some root objects
2. load a set of collection properties of the root objects
3. edit the loaded collections

Typically, we do the above several times where sometimes we load the same root objects/collections and sometimes different ones. Thereby, in point 1., we only load root objects which are not in the cache yet so I'm not sure whether this would "confuse" the subselect fetching since the query we issue might not contain all the root objects on which we want to load the collections in point 2.

In any case, I must say that your hint really helped and I will have a closer look at subselect fetching to see whether it fits our needs.

Still, I was wondering whether it would be useful if Hibernate provided a way to explicitly preload some property for a user-defined set of root objects since I think this would be extremely powerful when it comes to optimizing performance critical parts of an application? I just think that the existing fetching strategies do not always do what you want and the fetching strategies can also only be defined once globally for a given property.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 12:43 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
If you have an association from propertyName back to EntityClass then you can do a "from PropertyName p where p.entityClass.id in (x,y,z)" and pass a list of ids to load the propertyName object for a dynamically defined list of EntityClass objects.

The subselect always takes the query with which the list of root objects was loaded.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 2:35 pm 
Newbie

Joined: Wed Mar 04, 2009 6:36 pm
Posts: 8
pkleindl wrote:
If you have an association from propertyName back to EntityClass then you can do a "from PropertyName p where p.entityClass.id in (x,y,z)" and pass a list of ids to load the propertyName object for a dynamically defined list of EntityClass objects.

I do actually have such a back reference but if I execute the above query, Hibernate seems not to associate the loaded collections to the root objects so if I later on access the collections, additional queries are executed by Hibernate to retrieve the same collections again.


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