-->
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: How to build a HQL query with a parameter for IN clause
PostPosted: Thu Oct 20, 2005 6:41 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
Hi,

I try to create a HQL query that simulate the SQL query:

SELECT id FROM table_name WHERE id IN (501,502)

For the HQL query I want to pass for the IN clause a parameter :

SELECT c.id FROM class_name as c WHERE c.id IN ?

I don't know how to set the parameter (which type the parameter) for the IN clause.
If I set it like a String I get from Oracle the error invalid number.

Does anybody has an idea how passing a parameter for an IN clause ?

Thanks,
Cristian.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 6:51 am 
Newbie

Joined: Wed Apr 20, 2005 4:14 am
Posts: 10
Location: Netherlands
I think you should use:

Code:
where id in (?,?,?,...,?)

And bind the n parameters. (Note that Oracle only supports something like 1000 parameters.)

You should obviously know exactly how many parameters you're going to bind, and generate the string with the questionmarks yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 6:57 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
The problem is that I don't know before how many int parameters will be into the IN clause.

There isn't another solution at this ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 6:59 am 
Newbie

Joined: Wed Apr 20, 2005 4:14 am
Posts: 10
Location: Netherlands
Well, if the int-params are not from a List, Set of Array, where are they from?

Should it be the result of a subquery, you should obviously use:
Code:
...where id in (select distinct c.id from Cat c)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 7:02 am 
Beginner
Beginner

Joined: Mon Aug 16, 2004 6:15 am
Posts: 24
Have a look at Query.setParameterList().

This allows you to bind a List to a named parameter in a query.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 7:28 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
Thanks a lot,

It works very well with the CSwinney solution.

It works also with the Varienaja solutions but the subselect is also dynamic.

I appreciate your help,
Cristian.


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.