Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set list of values as parameter into hibernate query?

Tags:

sql

hibernate

jpa

For example, I have this query

 select cat from Cat cat where cat.id in :ids  

and I want to set ids to list (1,2,3,4,5,6,17,19).

This code doesn't work

session.createQuery("select cat from Cat cat where cat.id in :ids")        .setParameter("ids", new Long[]{1,2,3,4,5}) 

As the result I would like to have SQL query like id in (1,2,3,4)

like image 367
yura Avatar asked Jul 05 '11 15:07

yura


People also ask

What is query uniqueResult () in hibernate?

uniqueResult. Convenience method to return a single instance that matches the query, or null if the query returns no results.

What is query list in hibernate?

It is an object oriented representation of Hibernate Query. The object of Query can be obtained by calling the createQuery() method Session interface.


1 Answers

Use setParameterList(). You'll also have to put parenthesis around the list param.

session.createQuery("select cat from Cat cat where cat.id in (:ids)").setParameterList("ids", new Long[]{1,2,3,4,5}) 
like image 161
sblundy Avatar answered Sep 19 '22 15:09

sblundy