Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate HQL Query : How to set a Collection as a named parameter of a Query?

Given the following HQL Query:

FROM     Foo WHERE     Id = :id AND     Bar IN (:barList) 

I set :id using the Query object's setInteger() method.

I would like to set :barList using a List of objects, but looking at the Hibernate documentation and list of methods I cannot see an obvious choice of which to use. Any ideas?

like image 435
karlgrz Avatar asked Feb 20 '09 16:02

karlgrz


People also ask

How are named parameters specified in an HQL query?

It's use question mark (?) to define a named parameter, and you have to set your parameter according to the position sequence. See example… String hql = "from Stock s where s. stockCode = ? and s.

Can we use select * in HQL?

Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like table and column names are case sensitive in HQL.

What is named parameter in hibernate?

Named query parameters are tokens of the form :name in the query string. A value is bound to the integer parameter :foo by calling setParameter("foo", foo, Hibernate. INTEGER); for example. A name may appear multiple times in the query string.

How do you write or condition in HQL query?

The easiest way is to either create the hql string based on the value (if subServiceId is existent or not) or use the criteria api, thus you will just have to add one extra equals filter in case of subServiceId presence. The question should be changed on how to add dynamic conditions to hql based on variable presence.


1 Answers

Use Query.setParameterList(), Javadoc here.

There are four variants to pick from.

like image 60
Jason Cohen Avatar answered Sep 30 '22 06:09

Jason Cohen