Skip to content

Question mark in native query #2551

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

Closed
kubav182 opened this issue May 26, 2022 · 10 comments
Closed

Question mark in native query #2551

kubav182 opened this issue May 26, 2022 · 10 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL status: feedback-provided Feedback has been provided type: bug A general bug

Comments

@kubav182
Copy link

kubav182 commented May 26, 2022

When working with jsonb type in postgres, we can use ? operator.

SELECT * FROM table WHERE (json_col->'jsonKey')::jsonb ? :param

In JDBC driver you can use escape sequence ??
In Hibernate you can use escape sequence \\?\\?

But what should I use in Spring Data JPA? I'm getting error during startup java.lang.IllegalArgumentException: Mixing of ? parameters and other forms like ?1 is not supported!

I'm using Spring Boot 2.4.5

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 26, 2022
@schauder schauder added the in: query-parser Everything related to parsing JPQL or SQL label May 30, 2022
@schauder
Copy link
Contributor

This sounds like it needs fixing on our side. Could you provide a demonstrator, including the database using testcontainers, please?

@schauder schauder added the status: waiting-for-feedback We need additional information before we can continue label May 30, 2022
kubav182 pushed a commit to kubav182/demo-spring-data-jpa-question-mark that referenced this issue May 30, 2022
@kubav182
Copy link
Author

https://github.com/kubav182/demo-spring-data-jpa-question-mark

There is demo with working JPA and JDBC implementations. Data JPA implementation does not work and test fails.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 30, 2022
@gregturn gregturn self-assigned this May 31, 2022
@gregturn
Copy link
Contributor

Thanks @kubav182. I've reproduced it on my end with a very simple unit test.

@gregturn gregturn added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 31, 2022
@gregturn gregturn added this to the 3.0 M5 (2022.0.0) milestone May 31, 2022
gregturn added a commit that referenced this issue May 31, 2022
The checks for JDBC and JPA parameters were sloppy and based on side effects. By using zero width lookaheads, we can precisely spot situtations where the user has both types of parameters. Otherwise, let the query on through to the JPA provider.

Closes #2551.
@gregturn
Copy link
Contributor

@kubav182 I've patched it on Spring Data JPA 3.x where it ONLY blocks you if you REALLY have mixed JDBC and JPA style parameters. Anything else and we'll let it on through to the JPA provider. See commit above for examples.

gregturn added a commit that referenced this issue May 31, 2022
The checks for JDBC and JPA parameters were sloppy and based on side effects. By using zero width lookaheads, we can precisely spot situtations where the user has both types of parameters. Otherwise, let the query on through to the JPA provider.

Closes #2551.
gregturn added a commit that referenced this issue May 31, 2022
The checks for JDBC and JPA parameters were sloppy and based on side effects. By using zero width lookaheads, we can precisely spot situtations where the user has both types of parameters. Otherwise, let the query on through to the JPA provider.

Closes #2551.
@gregturn
Copy link
Contributor

Backported to 2.7.x and 2.6.x.

@kubav182
Copy link
Author

Nice work, looks good.

@gregturn
Copy link
Contributor

@kubav182 Are you able to share a little about how you've wired up Postgres, JSONB operations, your own custom queries, and what version of Spring Boot you are using to pull it all together?

We are hitting another issue in the land of Postgres and JSONB operations and hitting a roadblock.

@kubav182
Copy link
Author

@gregturn Sorry for late answer. We decided for another solution as we could not wait for release. I tried to upgrade my demo to Spring Boot 2.7.4 and it really does not work. I did investigation and the problem is in class org.springframework.data.jpa.repository.query.StringQuery and static field with regex.

public static final String POSITIONAL_OR_INDEXED_PARAMETER = "\\?(\\d*+(?![#\\w]))";

Prefix \\? matches question marks even they are escaped. If I skip this match, it works:

public static final String POSITIONAL_OR_INDEXED_PARAMETER = "[^\\\\]\\?(\\d*+(?![#\\w]))";

@Query(value = "select * from pet where (characteristics -> 'colours')\\:\\:jsonb \\?\\? :colour", nativeQuery = true)

I'm not sure about related risks.

@razorree
Copy link

Any updates on this? I still can't use ? jsonb operator in a native query (postgresql)
spring boot 2.7.18

@christophstrobl
Copy link
Member

@razorree spring-boot:2.7.x line is already out of OSS maintenance - if the problem is still there with a recent version, please open a new issue with more details. Ideally add a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: query-parser Everything related to parsing JPQL or SQL status: feedback-provided Feedback has been provided type: bug A general bug
Projects
None yet
Development

No branches or pull requests

6 participants