Parameter binding malfunctions when native query contains question marks
See original GitHub issueIn a PostgreSQL database I’ve set up a JSONB-column containing an array of string values, and my end goal is to use a native @Query in a Spring Data JPA repository to retrieve the entity where this column’s array contains a certain value.
Unfortunately, I’m stuck on what I assume is an issue with Spring Data JPA’s interpretation of the query, and more specifically, how it handles the ?-marks in the query. I tried using the escaping (\\?\\?) that was implemented in #2551, and while I don’t get the error message that was mentioned in that issue, I get others instead:
Case 1:
Required name for ParameterBinding [name: null, position: 1, expression: null] not available!
The query I’m trying to make work looks like this:
@Query(nativeQuery = true, value = """
select *
from PET
where TAGS_JSON -> 'tags' \\?\\? :tag
""")
Pet findByTag(String tag);
Case 2:
If I try to hardcode the value instead of passing it as a parameter, I get a different error message:
At least 1 parameter(s) provided but only 0 parameter(s) present in query.
Query:
@Query(nativeQuery = true, value = """
select *
from PET
where TAGS_JSON -> 'tags' \\?\\? '2222'
""")
Pet findByTagEquals2222();
I’ve put together a reproducible example project with some tests that illustrate the symptoms, and tried running them with Spring Data JPA 2.6.7 and 2.7.3. Both give the same results. Please let me know if there’s something I’m doing wrong 😃
Issue Analytics
- State:
- Created a year ago
- Comments:13 (6 by maintainers)
Top Related StackOverflow Question
Yeah, but from what I’ve read (I haven’t tested it myself,) the aliases don’t utilize the indexes. For the moment I’ve resorted to using
JdbcTemplatedirectly, but I’m still hoping this issue will get resolved someday too!Just a little notice that they replied in #2551: https://github.com/spring-projects/spring-data-jpa/issues/2551#issuecomment-1281699776