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?
2 Answers 2
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)
Since you are using the PostgreSQL JDBC driver, you should escape the question mark by doubling it: ??|
1 Comment
Explore related questions
See similar questions with these tags.
Spring JPA, but this Named Parameters looks like something worth trying.