Skip to content

The same named field assignment error in jpa native like query #2854

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
qindongliang opened this issue Mar 14, 2023 · 5 comments
Closed

The same named field assignment error in jpa native like query #2854

qindongliang opened this issue Mar 14, 2023 · 5 comments
Labels
status: duplicate A duplicate of another issue

Comments

@qindongliang
Copy link

qindongliang commented Mar 14, 2023

spring boot and jpa version is 2.5.3

query code:

    @Query(nativeQuery = true,
            value = "select userName as answer , LOCATE( :user, userName) as cnt from test where userName like %:user% ")
    List<GroupStatistics> testLikeQuery(@Param("user") String user);

enable logging trace in application.yml

logging:
  level:
    org.hibernate.type: TRACE

when my query user value is "tomcat", binding parameter logging show:

2023-03-14 11:41:34.042 [http-nio-8088-exec-1] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [%tomcat%]
2023-03-14 11:41:34.043 [http-nio-8088-exec-1] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [VARCHAR] - [%tomcat%]

The value of the first named parameter is wrong because it should not carry %%,

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 14, 2023
@quaff
Copy link
Contributor

quaff commented Mar 14, 2023

Duplication of #1929

@schauder schauder added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 14, 2023
@schauder
Copy link
Contributor

I agree, this is a duplicate.

@schauder schauder closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2023
@qindongliang
Copy link
Author

@quaff @schauder Has anyone fixed this?

@schauder
Copy link
Contributor

No, the linked issue is still open.

@klajdipaja
Copy link

klajdipaja commented Mar 16, 2023

@schauder I have opened a PR for #1929

EDIT: Closed the pull request as it did not sound like the best solution for the case.
My solution in the PR was to change the parameter names for queries that included repeated parameters but that would be misleading when looking the logs and the queries that where ran.
Example:
Query from user- > SELECT a FROM Article a WHERE a.title=:query OR a.overview LIKE %:query OR a.content LIKE %:escapedWord%
Transformed query - > SELECT a FROM Article a WHERE a.title=:query OR a.overview LIKE :query_ENDING_WITH OR a.content LIKE :query_CONTAINING

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants