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

When using queryWriter for pagination queries, the generated CountQuery is different from the Query and returns the error total #3801

Open
Chandlerigs opened this issue Mar 5, 2025 · 0 comments
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@Chandlerigs
Copy link

Chandlerigs commented Mar 5, 2025

If there are any areas that do not comply with the specifications when submitting an issue for the first time, please help me point them out

I use Spring Data REST and Spring Data JPA to complete my project.

When I use QueryRewrite to complete dynamic queries, there is an error in pagination.

My test demo link is as follows:
github Chandler : data-rest-test

@NoRepositoryBean
public interface BaseRepository<T, ID> extends JpaRepository<T, ID> {
    @Query(value = "select u from #{#entityName} u where 1 = 1", queryRewriter = DynamicQueryRewriter.class)
    Page<T> findByParams(@Nullable @Param("params") String params, @Nullable Pageable pageable);
}
public class DynamicQueryRewriter implements QueryRewriter {
    @Override
    public String rewrite(String query, Sort sort) {
        return query.replaceAll("1 = 1", " u.id >3 ");
    }
}

From the console, it can be seen that, Query SQLis different from countQuery SQL.

The Query SQL calls rewrite and modifies the where condition, while the countQuery SQL does not change, resulting in a total error

Image

There are 5 records in the database, 2 of which comply with the rule (id>3)
Image
When requesting the parameter size=2, the returned totalElements are 5,

Image

Image

When requesting the parameter size!=2 (eg: 4), The returned totalElements are 2,

Image

Image

Thank you for your reading and assistance

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants