Skip to content

Spring Data JDBC Pagination with Sort not working #2190

@sivaprasadreddy

Description

@sivaprasadreddy

I have an entity:

@Table("bookmarks")
public class Bookmark {

    @Id
    private Long id;

    @Column("title")
    private String title;

    @Column("category_id")
    private Long categoryId;

   // setters & getters
}

Repository for Bookmark:

public interface BookmarkRepository extends CrudRepository<Bookmark, Long>, ListPagingAndSortingRepository<Bookmark, Long> {

    Page<Bookmark> findByCategoryId(Long categoryId, Pageable pageable);
}

I am calling the findByCategoryId() method as follows:

Sort sort = Sort.by(Sort.Direction.DESC, "id");
Pageable pageable = PageRequest.of(pageNo-1, 10, sort);
Page<Bookmark> bookmarkPage =  bookmarkRepository.findByCategoryId(1L, pageable);

This is throwing the following error:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT COUNT(*) FROM "bookmarks" WHERE "bookmarks"."category_id" = ? ORDER BY "bookmarks"."id" DESC]
	....
	....
	at com.sivalabs.bookmarker.BookmarkServiceTest.getBookmarksByCategoryId(BookmarkServiceTest.java:17)
Caused by: org.postgresql.util.PSQLException: ERROR: column "bookmarks.id" must appear in the GROUP BY clause or be used in an aggregate function

This was working fine with Spring Boot 3.5.8.

Attaching the reproducer sample app.

spring-data-jdbc-issue.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions