-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Like operator with % does not work when checking nullable #2760
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've reproduced this against the latest version of Spring Data JPA. |
This problem appears to involve the query parser, and isn't simply confined to |
When the query is this:
...where there is simply the
However, when the query is this:
...then apparently the same process turns the query into:
In both situations, the "%" are stripped out of the query. However, in the first, the wildcards are moved to the bound variable. In the second, they aren't. At a glance, this looks like a bug. But I'm curious if we need this approach now that we have the query parsers in place. |
The problem is that you're using the same parameter in two different places. When Spring Data JPA does the parameter binding, if there is a Unfortunately, it turns into a race condition, in which the LAST instance of that parameter (
That's because the LAST binding done on One way to work around this is to put two different named parameters in the query, and feed the input twice. This will act as expected. As stated, this particular wrinkle of Spring Data JPA was implemented to move wildcards out of the query and into the bindings to ease a really complicated query pattern. The consequence is that we can't go back to standard JPQL (or HQL) and leave the wildcards in the query without breaking a lot of existing applications. I may explore if there is a backward compatible way to handle this scenario, but it will take time and effort. |
With a little more digging it has become apparent that the solution in your situation requires a rewrite like this:
By decoupling the wildcards from the parameter binding, Spring Data JPA will no longer attempt to "rewrite" your like condition and then later stomp on that with the I realize this may be confusing so I've opened #2939 to update the reference documentation and properly document this. Basically, If you rewrite your query as shown above you will sidestep all that risk and the query should operate as expected. |
Superceded by #2939. |
We support wrapping parameters (named or positional) with optional wildcards when doing LIKE patterns. This is out-of-band and requires moving the wildcards into the bindings. To stop doing this and causing race conditions, we can instead rewrite the queries using the CONCAT function. This function is standard across relational database (native queries) as well as JPA providers (Hibernate and EclipseLink). Resolves #2939. Related: #2760.
We support wrapping parameters (named or positional) with optional wildcards when doing LIKE patterns. This is out-of-band and requires moving the wildcards into the bindings. To stop doing this and causing race conditions, we can instead rewrite the queries using the CONCAT function. This function is standard across relational database (native queries) as well as JPA providers (Hibernate and EclipseLink). Resolves #2939. Related: #2760. Original Pull Request: #2944.
We support wrapping parameters (named or positional) with optional wildcards when doing LIKE patterns. This is out-of-band and requires moving the wildcards into the bindings. To stop doing this and causing race conditions, we can instead rewrite the queries using the CONCAT function. This function is standard across relational database (native queries) as well as JPA providers (Hibernate and EclipseLink). Resolves #2939. Related: #2760. Original Pull Request: #2940. Superceding Pull Request: #2944
We support wrapping parameters (named or positional) with optional wildcards when doing LIKE patterns. This is out-of-band and requires moving the wildcards into the bindings. To stop doing this and causing race conditions, we can instead rewrite the queries using the CONCAT function. This function is standard across relational database (native queries) as well as JPA providers (Hibernate and EclipseLink). See #2939 See #2760 Original Pull Request: #2940 Superceding Pull Request: #2944
We support wrapping parameters (named or positional) with optional wildcards when doing LIKE patterns. This is out-of-band and requires moving the wildcards into the bindings. To stop doing this and causing race conditions, we can instead rewrite the queries using the CONCAT function. This function is standard across relational database (native queries) as well as JPA providers (Hibernate and EclipseLink). See #2939 See #2760 Original Pull Request: #2940 Superceding Pull Request: #2944
We support wrapping parameters (named or positional) with optional wildcards when doing LIKE patterns. This is out-of-band and requires moving the wildcards into the bindings. To stop doing this and causing race conditions, we can instead rewrite the queries using the CONCAT function. This function is standard across relational database (native queries) as well as JPA providers (Hibernate and EclipseLink). See #2939 See #2760 Original Pull Request: #2940 Superceding Pull Request: #2944
I tried to compare optional string value using
@Query
method by containing and nullable like this;When this code calls in runtime, however, the Query remove the
%
operator like thisI found that this issue is solved when changing SQL order as checking nullable first, then containing, but I think this should be fixed.
My spring data jpa version is
2.6.10
.The text was updated successfully, but these errors were encountered: