Skip to content

Using || (pipes) along with named parameters in custom queries raises an exception [DATAJPA-1652] #1954

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
spring-projects-issues opened this issue Dec 28, 2019 · 4 comments
Assignees
Labels
in: core Issues in core support type: bug A general bug

Comments

@spring-projects-issues
Copy link

Andriy Redko opened DATAJPA-1652 and commented

Since the Lovelace release train, the usage of '||' along named parameters in custom queries ends up with Caused by: org.hibernate.QueryException: Named parameter [..] not set exception. The query is parsed and works as expected in Ingalls and before.

Example:

@Query(value = "SELECT u FROM User u WHERE u.lastname LIKE '%'||:name||'%'")
Page<User> findByNamedQueryWithLike(@Param("name") String name, Pageable page); 

The pipes (||) are supported by some SQL dialects (and HSQL) in order to support concatenation of multiple values, alternatively to concat function.

The cause: the identifier pattern (declared in QueryUtils) has changed in Lovelace and above, instead of capturing 'name', it captures 'name||', leading to:

Caused by: org.hibernate.QueryException: Named parameter [name] not set
        at org.hibernate.query.internal.QueryParameterBindingsImpl.verifyParametersBound(QueryParameterBindingsImpl.java:251)
        at org.hibernate.query.internal.AbstractProducedQuery.beforeQuery(AbstractProducedQuery.java:1313)
        at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1412)
        at org.hibernate.query.Query.getResultList(Query.java:146) 

 


Affects: 2.1.14 (Lovelace SR14), 2.2.3 (Moore SR3)

Referenced from: pull request #405

Backported to: 2.2.4 (Moore SR4), 2.1.15 (Lovelace SR15)

@spring-projects-issues
Copy link
Author

Jens Schauder commented

This problem is not just with | but also other characters that are considered punctuation by posix but not by unicode.

For example  > and < have the same problem.

See https://www.unicode.org/reports/tr18/#General_Category_Property
vs https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#posix

 An easy workaraound is to have a space between the parameter name and the following character

@spring-projects-issues
Copy link
Author

Andriy Redko commented

Thanks for looking Jens Schauder, I agree that pipes is not the only issue (it just what triggered problem for us). It is certainly good to have a workaround but from the other side, quite inconvenient, the whitespaces should not change the outcomes. I would be happy to improve the PR (https://octolinker-demo.now.sh/spring-projects/spring-data-jpa/pull/405) to exclude more punctuation characters from the identifier name (or include narrowed set of punctuation characters), what do you think? Thanks!

@spring-projects-issues
Copy link
Author

Jens Schauder commented

If you get a replacement of \P{P} with \P{Punct} to work I would appreciate such a PR

@spring-projects-issues
Copy link
Author

Andriy Redko commented

Thanks Jens Schauder, resubmitted the change with \P{Punct}, would appreciate your opinion, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants