0

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 name and argument type(s). You might need to add explicit type casts.

Here is my query

select * from table where value  like any (array[?1]);

when I run it using hibernate the query is like:

 select * from table where value  like any (array[('%foo%', '%bar%', '%baz%')]);

There's a best way to pass my array as parameter?? I think that is important to say that my array is dynamic so i can't fiz it in my query.

3 Answers 3

0
  • First use createNativeQuery instead of createQuery 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%'])
Sign up to request clarification or add additional context in comments.

2 Comments

Well, i've tried your suggestion but i can't use "... like any (array?1)" because i get the error: ERROR: syntax error at or near "?"
0

Firstly, your syntax is wrong.

Instead of:

select * from table where value  like any (array[?1]);

You should use:

select * from table where value like any (: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.

Comments

-1

use setParameterList method.

String queryStr = "select * from table where value in :valueList";

Query query = SessionFactory.getCurrentSession().createQuery(queryStr);


query.setParameterList("valueList", new Object[]{"%foo%","%bar%","%baz%"});

Please note that I used in clause and it doesn't support wildcard characters such as %.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.