-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Parameter binding malfunctions when native query contains question marks #2644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I should point out that these errors occur during calls to the repository methods, not during the context initialization. Unless I completely misunderstand the tests that were introduced as part of #2551, they only test context initialization, which might be how the errors escaped detection in that situation. |
Thanks for doing the extra research @ThomasKasene. I created two extra @Query("SELECT * FROM table WHERE (json_col->'jsonKey')::jsonb \\?\\? '2222'")
List<User> customQueryWithQuestionMarksAndNoArgs();
@Query(nativeQuery = true, value = "SELECT * FROM table WHERE (json_col->'jsonKey')::jsonb \\?\\? '2222'")
List<User> customQueryWithQuestionMarksAndNoArgsAndNative(); Turns out, the first one works just fine but the second one blows up as an invalid query. This suggests the problem is our native query handler. Looking back at all the test cases written, not one uses native queries. |
For native queries, we delegate to JSqlParser, and in this scenario, it's JSqlParser that is throwing an exception:
It turns out, you're not the first one to ask for support from JSqlParser to support such a query. See JSQLParser/JSqlParser#1434 |
Hmm... If I try to replicate your query, but in that example project I linked to, then the context initialization fails. Note that I'm no PostgreSQL expert and I don't know whether this change actually makes any sense: @Query(value = """
select *
from PET
where (TAGS_JSON -> 'tags')::jsonb \\?\\? :tag
""")
Pet findByTag(String tag);
|
Sorry, I was running these tests against Spring Data JPA 3.0 ( Method otherMethod = UserRepository.class.getMethod("customQueryWithQuestionMarksAndNoArgs", String.class);
RepositoryMetadata otherMetadata = new DefaultRepositoryMetadata(UserRepository.class);
strategy.resolveQuery(otherMethod, otherMetadata, projectionFactory, namedQueries); @Query(value = "select * from PET where (TAGS_JSON -> 'tags')::jsonb \\?\\? :tag")
List<User> customQueryWithQuestionMarksAndNoArgs(String tag); My test case went green. Naturally, I switched back to Java 8 to do this test run and dropped the multi-line string to a single line string. |
Let me grab the reproducer and work from there. |
Hmm...I can't tell where the problem is. I reached out to the person involved in #2551, because that solution worked from them, but they never posted any details. |
Thanks for your efforts so far 😃 Let me know if there's anything else I can do, although I hope it won't come to submitting a PR with JSqlParser project! At the risk of going a little off-topic (but it's the reason why I'm defining my query the way I am,) I'm wondering if you know whether the suggested query (
|
I frankly don't know Postgres handles these. My instinct would be somewhere along the way, surely there's an opportunity to hit an index. I guess I'd have to dig in deep on Google Fu, or at least the Postgres user groups. I never managed indexes directly. We always had someone from DBA team (previous position before Spring team) to do that and suitably coach us. |
Just a little notice that they replied in #2551: |
The best solution/workaround which worked for me was not to use the |
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 |
You are right, the function does not hit the index, while "?" operator does. There is workaround to implement this select via JdbcTemplate or EntityManager. I provided solution to fix this error in Spring Data JPA #2551 as mentioned above. |
This situation appears too complicated for Your best best moving forward may be the custom implementation you've to implement a custom implementation. Check out https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations if you need more details about how that works. |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the 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:
The query I'm trying to make work looks like this:
Case 2:
If I try to hardcode the value instead of passing it as a parameter, I get a different error message:
Query:
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 😃
The text was updated successfully, but these errors were encountered: