Skip to content
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

Pageable Always Adding From Entity to Sort Field Name #2960

Closed
krankkkk opened this issue May 19, 2023 · 4 comments
Closed

Pageable Always Adding From Entity to Sort Field Name #2960

krankkkk opened this issue May 19, 2023 · 4 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL type: regression A regression from a previous release

Comments

@krankkkk
Copy link

krankkkk commented May 19, 2023

Regression of #1066

Error now reoccurs on Spring Boot 3.1

When using @Query in a JPA repository along with Pageable and attempting to sort on a field in a joined entity, the from entity gets prepended to the sort field. For example, if I do the following query:

@Query(value = "select p from Customer c join c.productOrder p where p.delayed = true")

If I pass a PageRequest sorting on a field in the from entity (Customer), everything will work fine:

PageRequest page = new PageRequest(0, 20, Direction.DESC, "lastName");

However, if I simply decide to sort by a field in the ProductOrder entity with this PageRequest:

PageRequest page2 = new PageRequest(0, 20, Direction.DESC, "p.lineItems");

I will get the following error:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.query.sqm.InterpretationException: Error interpreting query [select p from Customer c join c.productOrder p where p.delayed = true order by c.p.lineItems desc]; this may indicate a semantic (user query) problem or a bug in the parser [select p from Customer c join c.productOrder p where p.delayed = true order by c.p.lineItems desc]
   <snip>
Caused by: java.lang.IllegalArgumentException: org.hibernate.query.sqm.InterpretationException: Error interpreting query [select p from Customer c join c.productOrder p where p.delayed = true order by c.p.lineItems desc]; this may indicate a semantic (user query) problem or a bug in the parser [select p from Customer c join c.productOrder p where p.delayed = true order by c.p.lineItems desc]
   <snip>
Caused by: org.hibernate.query.sqm.InterpretationException: Error interpreting query [select p from Customer c join c.productOrder p where p.delayed = true order by c.p.lineItems desc]; this may indicate a semantic (user query) problem or a bug in the parser [select p from Customer c join c.productOrder p where p.delayed = true order by c.p.lineItems desc]
   <snip>
Caused by: java.lang.IllegalArgumentException: org.hibernate.query.SemanticException: Could not resolve attribute 'p' of 'hello.Customer'
   <snip>
Caused by: org.hibernate.query.SemanticException: Could not resolve attribute 'p' of 'hello.Customer'
   <snip>

c. was prepended onto the sort field.

Updated reproducer project: Archive.zip

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 19, 2023
@mp911de mp911de added type: regression A regression from a previous release in: query-parser Everything related to parsing JPQL or SQL and removed status: waiting-for-triage An issue we've not yet triaged labels May 22, 2023
gregturn added a commit that referenced this issue May 22, 2023
JOIN clauses can have aliases as well, despite not using an AS reserved word. The HQL query parser needs to handle this.

See #2960, #1066, #664.
gregturn added a commit that referenced this issue May 22, 2023
JOIN clauses can have aliases as well, despite not using an AS reserved word. The HQL query parser needs to handle this.

See #2960, #1066, #664
Original Pull Request: 2967
@gregturn gregturn added this to the 3.2 M1 (2023.1.0) milestone May 22, 2023
@gregturn
Copy link
Contributor

gregturn commented May 22, 2023

Looks like this issue not only regresses against #1066, but touches on #664, an older ticket that may have been the origin of handling JOINs with aliases in Sorts.

By registering JOIN aliases with the same mechanism as SELECT aliases, we are easily able to handle any Sort property that starts with an alias and have them skip getting the FROM alias applied. Feel free to review the linked PR.

gregturn added a commit that referenced this issue May 30, 2023
JOIN clauses can have aliases as well, despite not using an AS reserved word. The HQL query parser needs to handle this.

See #2960, #1066, #664
Original Pull Request: 2967
gregturn added a commit that referenced this issue May 30, 2023
JOIN clauses can have aliases as well, despite not using an AS reserved word. The HQL query parser needs to handle this.

See #2960, #1066, #664
Original Pull Request: 2967
@gregturn
Copy link
Contributor

gregturn commented Jun 6, 2023

This is certainly impacted by #3002.

gregturn added a commit that referenced this issue Jun 6, 2023
JOIN clauses can have aliases as well, despite not using an AS reserved word. The HQL query parser needs to handle this.

See #2960, #1066, #664
Original Pull Request: 2967
gregturn added a commit that referenced this issue Jun 12, 2023
JOIN clauses can have aliases as well, despite not using an AS reserved word. The HQL query parser needs to handle this.

See #2960, #1066, #664
Original Pull Request: 2967
@gregturn gregturn changed the title Regression: Pageable Always Adding From Entity to Sort Field Name Pageable Always Adding From Entity to Sort Field Name Jun 12, 2023
@gregturn
Copy link
Contributor

Based on #3010 (comment), #3002 is no longer relevant.

gregturn added a commit that referenced this issue Jun 12, 2023
JOIN clauses can have aliases as well, despite not using an AS reserved word. The HQL query parser needs to handle this.

See #2960, #1066, #664
Original Pull Request: 2967
gregturn added a commit that referenced this issue Jun 12, 2023
JOIN clauses can have aliases as well, despite not using an AS reserved word. The HQL query parser needs to handle this.

See #2960, #1066, #664
Original Pull Request: 2967
@gregturn
Copy link
Contributor

Merged to main and backported to 3.1.x.

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 type: regression A regression from a previous release
Projects
None yet
4 participants