-->
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.  [ 6 posts ] 
Author Message
 Post subject: Best way to get large number of objects by Id
PostPosted: Fri Aug 12, 2005 5:46 am 
Newbie

Joined: Fri Aug 12, 2005 5:41 am
Posts: 5
HI,

I need to retrieve a large collection of persistent objects having an array of primary keys (java.lang.Long).
Those objects are quite long to load and I was wondering what is the most efficient query to do that.
Thanks,

Thomas


Top
 Profile  
 
 Post subject: Use 'where key in (1,2,3,4)'
PostPosted: Fri Aug 12, 2005 6:25 am 
Beginner
Beginner

Joined: Wed Jan 19, 2005 6:07 am
Posts: 20
You can use a query like this :

" from table where key in (?)"

and making the parameter a string of the complete list of keys you have

StringUtils.join(keyArray,",") (from the apache.commons-package).

This will load all objects with the keys in the key-array


Top
 Profile  
 
 Post subject: Re: Use 'where key in (1,2,3,4)'
PostPosted: Fri Aug 12, 2005 6:57 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
frodeh wrote:
You can use a query like this :

" from table where key in (?)"

and making the parameter a string of the complete list of keys you have

StringUtils.join(keyArray,",") (from the apache.commons-package).

This will load all objects with the keys in the key-array


Taking the Jakarta Commons is redundant in this case.

Code:
Long[] keys = ...;
Query query = session.createQuery("select c from Class c where c.key in (:keys)");
query.setParameterList("keys", keys);


Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 7:31 am 
Newbie

Joined: Fri Aug 12, 2005 5:41 am
Posts: 5
Thanks for your responses!
I tried Sven's solution and it works like a charm!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 8:16 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Be careful, in queries are limited to a certain number of values, this limit is dependant of your database.

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 8:18 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
thogau wrote:
Thanks for your responses!
I tried Sven's solution and it works like a charm!


You're welcome to rate answers helpful which helped solving your problem. :)

Best regards
Sven


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