Skip to content

Pageable.Sort problem in Spring Boot 2.6.7 and Spring Boot 2.5.13 #30867

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
fuxao opened this issue May 4, 2022 · 4 comments
Closed

Pageable.Sort problem in Spring Boot 2.6.7 and Spring Boot 2.5.13 #30867

fuxao opened this issue May 4, 2022 · 4 comments
Labels
for: external-project For an external project and not something we can fix

Comments

@fuxao
Copy link

fuxao commented May 4, 2022

Description of Pageable.Sort problem

Introduction

I have a controller where @SortDefault is used.

@SortDefault(sort="updated", direction = Sort.Direction.DESC) Pageable pageable

There are 2 separate entities without connection. There is a query in DocumentRepository (JpaRepository) in which both entities are used.

@Query("select d from Document d where " +
            "(:name is null or d.name = :name) and " +
            "(:machineExternalId is null or exists (select machine.id from Machine machine where machine.externalId = :machineExternalId))"
    )
Page<DocumentEntity> findAllBy(String name, String machineExternalId, Pageable pageable);

In real application this query is much more complicated, I simplify this query for example application.

Problem report

Before Spring Boot 2.6.7 and 2.5.13 this query generated this SQL statement.

select documenten0_.id as id1_0_, documenten0_.name as name2_0_, documenten0_.updated as updated3_0_ from document documenten0_ where (? is null or documenten0_.name=?) and (? is null or exists (select machineent1_.id from machine machineent1_ where machineent1_.external_id=?)) order by documenten0_.updated desc limit ?

As you can see the sql statement ends with order by documenten0_.updated. This is expected behaviour. After upgrading to Spring Boot 2.6.7 or 2.5.13 QuerySyntaxException is thrown instead.

org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'machine.updated' 
[select d from org.example.entity.DocumentEntity d where 
(:name is null or d.name = :name) and 
(:machineExternalId is null or exists (select machine.id from org.example.entity.MachineEntity machine where machine.externalId = :machineExternalId)) 
order by machine.updated desc]

As you can see the problem is that the query ends with order by machine.updated. This is not OK. Incorrectly machine entity is used.

Example project

I attach maven project (as simple as possible) in which the problem can be debug.

pageable-sort-problem.zip

You can see in pom.xml tested spring boot versions and the results of my tests.

        <!--        <version>2.6.7</version> PROBLEM -->
        <!--        <version>2.6.6</version> OK -->
        <!--        <version>2.6.5</version> OK -->
        <!--        <version>2.6.1</version> OK -->
        <!--        <version>2.5.13</version> PROBLEM -->
        <!--        <version>2.5.12</version> OK -->
        <!--        <version>2.4.10</version> OK -->
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 4, 2022
@scottfrederick
Copy link
Contributor

@fuxao This appears to be a Spring Data JPA problem, possibly related to spring-projects/spring-data-jpa#2260. It's difficult to verify with your sample because it does not include a failing test that reproduces the problem.

Spring Boot 2.6.7 upgraded to Spring Data BOM 2021.1.4, which includes Spring Data JPA 2.6.4. You can configure your build to use Boot 2.6.7 while downgrading Spring Data BOM to 2021.1.3 (and therefore Spring Data JPA 2.6.3) by setting a property in pom.xml like the one below.

    <properties>
        ...
        <spring-data-bom.version>2021.1.3</spring-data-bom.version>
    </properties>

Can you try this and see if it works around your problem?

@scottfrederick scottfrederick added the status: waiting-for-feedback We need additional information before we can continue label May 4, 2022
@fuxao
Copy link
Author

fuxao commented May 4, 2022

@scottfrederick You are right. Everything is OK with the properties setting <spring-data-bom.version>2021.1.3</spring-data-bom.version>. This workaround solves the problem.

After reading spring-projects/spring-data-jpa#2260 I think that it describes the same problem.

I also updated the example project and now there is one failing test that reproduces the problem.

pageable-sort-problem.zip

By default the properties setting for spring-data-bom is in pom.xml as comment . In this case the result is NOT OK.

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

If you uncomment this properties setting, everything is OK.

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 4, 2022
@scottfrederick
Copy link
Contributor

Thanks very much for following up @fuxao. I'll close this issue as a duplicate of the Spring Data JPA issue. It looks like you should see the fix in the next round of Spring Data releases and the Spring Boot releases that follow.

@scottfrederick scottfrederick added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels May 4, 2022
@wilkinsona
Copy link
Member

@xuekvm You can learn how to do it with Gradle in the plugin's documentation. If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

4 participants