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

Don't prefix ORDER BY properties when HQL doesn't have a primary alias. #2971

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-gh-2969-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-gh-2969-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-gh-2969-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-gh-2969-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-gh-2969-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-gh-2969-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

/**
* Transformational operations needed to support either {@link HqlQueryTransformer} or {@link JpqlQueryTransformer}.
Expand Down Expand Up @@ -103,7 +104,7 @@ private void checkSortExpression(Sort.Order order) {
*/
private String generateOrderByArgument(@Nullable String primaryFromAlias, Sort.Order order) {

if (shouldPrefixWithAlias(order)) {
if (shouldPrefixWithAlias(order, primaryFromAlias)) {
return primaryFromAlias + "." + order.getProperty();
} else {
return order.getProperty();
Expand All @@ -115,9 +116,15 @@ private String generateOrderByArgument(@Nullable String primaryFromAlias, Sort.O
* FROM clause's alias.
*
* @param order
* @param primaryFromAlias
* @return boolean whether or not to apply the primary FROM clause's alias as a prefix
*/
private boolean shouldPrefixWithAlias(Sort.Order order) {
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("(")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,13 @@ void shouldHandleAliasInsideCaseStatement() {
"order by newDateDue desc");
}

@Test // GH-2969
void fromWithoutAPrimaryAliasShouldWork() {

assertThat(createQueryFor("FROM Story WHERE enabled = true", Sort.by(Sort.Direction.DESC, "created")))
.isEqualTo("FROM Story WHERE enabled = true order by created desc");
}

private void assertCountQuery(String originalQuery, String countQuery) {
assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
}
Expand Down