1

I need to search entities where at least one element from array exists in jsonb array. For this I can use an sql query:

select * from person where roles ?| array['ROLE_1','ROLE_2'];

But in case of spring jpa this is not valid:

@Query(value = "select * from person where roles ?| array['ROLE_1','ROLE_2']", nativeQuery = true)

The error is following:

At least 1 parameter(s) provided but only 0 parameter(s) present in query.

I understand that the problem is in special char ? which spring interpret as a required parameter in repository method (say there is method findRole1OrRole2()), but how I can handle that?

asked Jan 27, 2021 at 18:34
1
  • I don't use Spring JPA, but this Named Parameters looks like something worth trying. Commented Jan 27, 2021 at 19:03

2 Answers 2

3

I found the internal implementation of operation ?|, under the hood it invokes the procedure jsonb_exists_any(jsonb , text[]), so the possible solution is:

@Query(value = "select * from person where jsonb_exists_any(roles, array['ROLE_1','ROLE_2'])", nativeQuery = true)
answered Jan 27, 2021 at 19:12
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't exactly solve the problem, because when You use functions (instead of operators: ?, ?| etc.), Postgresql doesn't use indecies !!
this works perfectly at least on less amount of data.
1

Since you are using the PostgreSQL JDBC driver, you should escape the question mark by doubling it: ??|

answered Jan 27, 2021 at 18:39

1 Comment

Thanks for comment! It doesn't work, there is the same error msg

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.