I am trying to create a native query using spring JPA for the below query. I am trying to filter the data where the jsonB contains the filtered key.
select * FROM targeted_event_catalog e WHERE e.severity_map ? 'serviceName';
So far I have done this
@Query(value = "SELECT * FROM targeted_event_catalog e WHERE e.severity_map ? :service", nativeQuery = true)
List<TargetedEventRegistration> findByService(@Param("service") String service);
But I get the below error
Caused by: java.lang.IllegalArgumentException: Mixing of ? parameters and other forms like ?1 is not supported
The severity_map in a postgres column is a jsonb type. This is how data looks,
{"serviceName": ["info", "warning", "critical"]}
I wanted to know anything missing here? I have also tried adding two ?? but it did not work and threw the same error.
Thanks to @Zegarek comment. I was able to fix this. I used jsonb_exists(e.severity_map,?1) (passing service name as a param) and this fixed the issue. final query
"SELECT * FROM targeted_event_catalog e WHERE jsonb_exists(e.severity_map,?1)"