Skip to content

QueryRewriter not applied to count queries #3801

Closed
@Chandlerigs

Description

@Chandlerigs

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions