Parameter binding malfunctions when native query contains question marks

See original GitHub issue

In 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:open
  • Created a year ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomasKasenecommented, Nov 18, 2022

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 JdbcTemplate directly, but I’m still hoping this issue will get resolved someday too!

1reaction
ThomasKasenecommented, Nov 3, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

PDO statements with named parameters VS question mark ...
I'll be providing either question marks or the parameters' names in the query string. I want to make a separation to provide addition...
Read more >
Problems with question marks in operators (JDBC, ECPG, ...)
Hello, I've been trying to use the new JSONB format using JDBC, and ran into trouble with the question mark operators (?, ?|...
Read more >
Hibernate parameter binding examples - Mkyong.com
It's use question mark (?) to define a named parameter, and you have to set your parameter according to the position sequence. See...
Read more >
7 Performing SQL Operations with Native Dynamic SQL
Passing Schema Object Names As Parameters ... Use concatenation to build the string, rather than trying to pass the table name as a...
Read more >
Chapter 14. Creating and executing queries
Prepare the query: bind runtime arguments to query parameters, set hints, ... Hibernate also has its own SQL result-mapping facility, with org.hibernate.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found