-->
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 to load by many IDS at once w/o calling get each time
PostPosted: Fri Jul 14, 2006 12:12 pm 
Newbie

Joined: Thu Jun 30, 2005 10:35 am
Posts: 13
Hi, I just have a quick question and I'm hoping someone can give me a hand...

In my code I have a Set of IDs which correspond to the IDs for a specific object that I want to load and I was wondering if I could easily do this in one database call rather than iterating through the set of IDs and calling session.get on each one.

Right now my code looks like:
for(Long id : idSet) {
session.get(MyObject, id)
}

Basically what I'm looking for is similar to this SQL:

select * from MYTABLE where id in (... my id set ...)

Does anyone know how to do this in one call?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 12:32 pm 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
Criteria criteria = session.createCriteria(YourObjectClass.class);

criteria.add(Expression.equal("id", id???));

return criteria.list();

That will return a list with ID's where the id??? is equal to a given ID?

Then you can just iterate through your list which it returns?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 1:21 pm 
Newbie

Joined: Thu Jun 30, 2005 10:35 am
Posts: 13
Mr Jones, thanks a lot for your help.

What you are saying is exactly what I'm looking to do--to create one criteria or load/get statement which will take in a set of ids rather than a single id.

My only question, though, is about the syntax that you typed. What do the question marks represent where you type id???

If my set is named idSet then do I use
Expression.equal("id",idSet)
or am I supposed to use Expression.equal("id",idSet???)

The syntax doesn't make much sense to me.

Thanks again for your help...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 5:07 pm 
Newbie

Joined: Fri Oct 14, 2005 4:42 pm
Posts: 15
Try hql:

Code:
List result = session.createQuery("from MyObject where id in (:idSet)").setParameterList("idSet", idSet).list();


Or criteria query:

Code:
List result = session.createCriteria(MyObject.class).add(Restrictions.in("id", idSet)).list();


If you find this useful, please award me a point!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 12:44 pm 
Newbie

Joined: Thu Jun 30, 2005 10:35 am
Posts: 13
Mikekonikoff, thanks for your help. "Restrictions.in" is exactly what I was looking for!

Dave


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.