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?
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.
Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like table and column names are case sensitive in HQL.
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.
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.
Use Query.setParameterList()
, Javadoc here.
There are four variants to pick from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With