Skip to content

@Query with Sort/Pageable not adding aliases properly in ORDER BY clause #3054

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
rodrigo-jaris opened this issue Jul 5, 2023 · 3 comments
Closed
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL type: bug A general bug

Comments

@rodrigo-jaris
Copy link

If one is using aliases in the where clause and the property name added to the Sort/Pageable starts with any of the where clause aliases, spring-data-jpa won't add the alias to the ORDER BY clause. In the application linked below, we are sorting by testDuplicateColumnName and in the where clause we have a JOIN with an alias t. Thus, since testDuplicateColumnName starts with t, spring-data-jpa won't add the alias t3. as it should add to the generated ORDER BY clause. The consequence of not adding the alias is that the generated query will FAIL to be executed by Hibernate. It started happening in our application after migration to SpringBoot 3.1.1 and consequently Spring Data JPA 3.1.1.

That happens because IMO there is a bug in the following class: org.springframework.data.jpa.repository.query.JpaQueryTransformerSupport. The method that has the bug is the following:

private boolean shouldPrefixWithAlias(Sort.Order order, String primaryFromAlias) {

     // If there is no primary alias
	if (ObjectUtils.isEmpty(primaryFromAlias)) {
		return false;
     }

     // If the Sort contains a function
	if (order.getProperty().contains("(")) {
		return false;
     }

     // If the Sort references an alias directly
	if (projectionAliases.contains(order.getProperty())) {
		return false;
     }

     // If the Sort property starts with an alias
     // THIS IS THE BUG. IMHO A "." MISSING AND IT SHOULD BE: alias -> order.getProperty().startsWith("%s.".formatted(alias))
	if (projectionAliases.stream().anyMatch(alias -> order.getProperty().startsWith(alias))) {
		return false;
     }
	return true;*
}

Sample application reproducing the issue: https://github.com/rodrigo-jaris/spring-data-jpa-query-bug. This application can be downloaded and there is a test that can be run to show the error happening.

Also, to make the problem worse, we can't create the Sort by manually adding the Alias, as spring-data-jpa will duplicate the alias in that case.

The same issue happens when we have a query with aliases that work without manually adding the alias to the Sort

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 5, 2023
@gregturn gregturn self-assigned this Jul 5, 2023
@gregturn gregturn added type: bug A general bug in: query-parser Everything related to parsing JPQL or SQL and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 5, 2023
@gregturn gregturn added this to the 3.1.2 (2023.0.2) milestone Jul 5, 2023
@gregturn
Copy link
Contributor

gregturn commented Jul 5, 2023

I've confirmed the bug, thanks to your reproducer.

gregturn added a commit that referenced this issue Jul 5, 2023
When using an alias in a query, verify using "." as a proper separator against both alias projections as the primary alias to ensure that a Sort property doesn't overlap with an alias.

See #3054
gregturn added a commit that referenced this issue Jul 5, 2023
When using an alias in a query, verify using "." as a proper separator against both alias projections as the primary alias to ensure that a Sort property doesn't overlap with an alias.

See #3054
@gregturn gregturn closed this as completed Jul 5, 2023
@gregturn
Copy link
Contributor

gregturn commented Jul 5, 2023

Merged to main and backported to 3.1.x

@rodrigo-jaris
Copy link
Author

@gregturn, thanks for the fix. Do you have any clue when 3.1.2 is planned to be out? Thank you!

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: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants