How To Using Like Array Of String Hibernate
I'm trying to pass an array of string as a parameter to my query but I get the following error ERROR: operator does not exist: text ~~ record Dica: No operator matches the given n
Solution 1:
use setParameterList method.
StringqueryStr="select * from table where value in :valueList";
Queryquery= SessionFactory.getCurrentSession().createQuery(queryStr);
query.setParameterList("valueList", newObject[]{"%foo%","%bar%","%baz%"});
Please note that I used in clause and it doesn't support wildcard characters such as %.
Solution 2:
- First use
createNativeQuery
instead ofcreateQuery
as syntax is native to PSQL. - Second query syntax should be
select * from table where value like any (array?1)
as?1
will be substituted by['%foo%', '%bar%', '%baz%']
, so your end query will match required PSQL syntax.select * from table where value like any (array['%foo%', '%bar%', '%baz%'])
Solution 3:
Firstly, your syntax is wrong.
Instead of:
select*fromtablewherevaluelikeany (array[?1]);
You should use:
select*fromtablewherevaluelikeany (:vals);
You cannot use array[?]
or array[:var]
to construct the variable. That is invalid syntax.
Secondly for Hibernate 5.2 up to 5.4 you can just add this dependency and you can use the most common object arrays directly. Primitive arrays are not supported and are not supposed to be.
Post a Comment for "How To Using Like Array Of String Hibernate"