So, here is the native MySQL query where I want to get all the rows based on JSON key-value pairs (ev_data is json type column), and it works just fine:
SELECT * FROM event WHERE JSON_CONTAINS(ev_data, '{"key":"key1","value":"val1"}','$.items')
An example of ev_data column content:
{"items":[{"key":"key1","value":"val1"},{"key":"key2","value":"val2"}]}
Now, I am using Spring Boot's CrudRepository interface and @Query annotation for my custom fetch method as follows:
@Query(value="SELECT * FROM event WHERE JSON_CONTAINS(ev_data, '{\"key\" : \"key1\", \"value\" : :value}', '$.items')", nativeQuery=true)
Set<Event> findAllByItemKey1Value(@Param("value") String value);
This, however, won't work and trigger an exception like:
java.lang.IllegalArgumentException: Parameter with that name [value] did not exist because, apparently, you are not allowed to use parameters (named or indexed) inside quoted strings. Not sure if it's JPA/JPQL or Spring Boot thing but it is a fact.
'{\"key\" : \"key1\", \"value\" : :value}'... and the parameter is in that, so hence is treated a string literal NOT a parameter. The message tells you exactly that.