File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes How to use IN clause in HQL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to use IN clause in HQL?" Watch "How to use IN clause in HQL?" New topic

How to use IN clause in HQL?

Ganesh Ramani

Joined: Aug 07, 2007
Posts: 20

I am looking to use IN clause in my HQL. I referred some forums and couldnt get an optimal idea for this.

One approach i tried is to set the parameter of HQL as shown below:

My HQL: SELECT prd FROM ProductORM AS prd WHERE prd.productID in (:selPrdIDs)

Java code: hqlQuery.setParameterList("selPrdIDs", prdIdList); //prdIdList is List<Integer>

But this resulted in SQLGrammarException: could not execute query ; SQLException: Invalid parameter binding(s)

Please help to implement IN clause in HQL...

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

That is the best approach. And that code should work. I might be missing something simple in it, but it looks fine to me. I'd only ask to make sure that the List had values and was not null.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Steven Lawrence Hunter

Joined: Apr 17, 2008
Posts: 4
Use org.hibernate.Query.setParameterList(), which can handle a Collection or an Object[].
Ganesh Ramani

Joined: Aug 07, 2007
Posts: 20
Yeah, I discovered that after few day I posted that. Nevertheless, thenks for your support :-)
I agree. Here's the link:
subject: How to use IN clause in HQL?
It's not a secret anymore!