You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
privatebooleanshouldPrefixWithAlias(Sort.Orderorder, StringprimaryFromAlias) {
// If there is no primary aliasif (ObjectUtils.isEmpty(primaryFromAlias)) {
returnfalse;
}
// If the Sort contains a functionif (order.getProperty().contains("(")) {
returnfalse;
}
// If the Sort references an alias directlyif (projectionAliases.contains(order.getProperty())) {
returnfalse;
}
// 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))) {
returnfalse;
}
returntrue;*
}
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
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
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 aJOIN
with an aliast
. Thus, sincetestDuplicateColumnName
starts witht
,spring-data-jpa
won't add the aliast3.
as it should add to the generatedORDER BY
clause. The consequence of not adding the alias is that the generated query willFAIL
to be executed by Hibernate. It started happening in our application after migration toSpringBoot 3.1.1
and consequentlySpring 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: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, asspring-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
The text was updated successfully, but these errors were encountered: