Skip to content

JdbcAggregateTemplate.findAll with Query and Pageable issue #2138

@Alterant-zz

Description

@Alterant-zz

The JdbcAggregateTemplate.findAll(Query query, Class domainType, Pageable pageable) accept Pageable, but Query can have it's own limit\offset\sort params set.
When this happens an incorrect SQL is generated.

I think it's worth to at least mention in the javadoc that Query should not have any limit\offset\sort params for this method.

Possible fixes:

  • An exception in this case
  • Pageable should fully override limit\offset\sort params of the Query

Example:

Criteria criteria = Criteria.where(ParentEntity.COLUMN_ACTIVE).is(true);
Query query = Query.query(criteria)
    .offset(1)
    .limit(3)
    .sort(Sort.by(Sort.Order.desc(ParentEntity.COLUMN_TITLE).nullsFirst()));

Page<ParentEntity> page = operations.findAll(
    query, ParentEntity.class,
    PageRequest.of(1, 2, Sort.Direction.ASC, ParentEntity.COLUMN_TITLE)
);

generates a query where limit and offset is from Pageable, but sorting includes both criteria

SELECT "parent_entity"."id" AS "id", "parent_entity"."title" AS "title", "parent_entity"."active" AS "active"
FROM "parent_entity"
WHERE "parent_entity"."active" = ?
ORDER BY
  "parent_entity"."title" DESC NULLS FIRST,
  "parent_entity"."title" ASC
LIMIT 2 OFFSET 2

and COUNT have all criteria from Query including limit and offset and does not work at least in Postgresql

SELECT COUNT(1)
FROM "parent_entity"
WHERE "parent_entity"."active" = ?
ORDER BY "parent_entity"."title" DESC NULLS FIRST
LIMIT 3 OFFSET 1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions