{"id":19882978,"url":"https://github.com/maemresen/spring-pagination-example","last_synced_at":"2025-10-11T17:10:22.363Z","repository":{"id":189791646,"uuid":"596634501","full_name":"maemresen/spring-pagination-example","owner":"maemresen","description":"Discover Spring Data pagination: optimal querying for complex relationships, accurate strategies, and practical examples. Elevate your projects with robust pagination techniques.","archived":false,"fork":false,"pushed_at":"2023-02-02T16:28:16.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-01T03:26:06.372Z","etag":null,"topics":["best-practices","code-examples","entity-relationship","hibernate","java-persistence-api","pagination","spring-data"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maemresen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-02-02T16:01:54.000Z","updated_at":"2023-08-20T20:21:42.000Z","dependencies_parsed_at":"2023-08-21T19:55:43.530Z","dependency_job_id":null,"html_url":"https://github.com/maemresen/spring-pagination-example","commit_stats":null,"previous_names":["maemresen/spring-pagination-example"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/maemresen/spring-pagination-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maemresen%2Fspring-pagination-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maemresen%2Fspring-pagination-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maemresen%2Fspring-pagination-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maemresen%2Fspring-pagination-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maemresen","download_url":"https://codeload.github.com/maemresen/spring-pagination-example/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maemresen%2Fspring-pagination-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279008111,"owners_count":26084396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["best-practices","code-examples","entity-relationship","hibernate","java-persistence-api","pagination","spring-data"],"created_at":"2024-11-12T17:19:04.927Z","updated_at":"2025-10-11T17:10:22.346Z","avatar_url":"https://github.com/maemresen.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction;\nWhen you want to apply pagination with using Spring Data, you could define a method in your repository something as follows;\n\n````java\npublic interface ...extends JpaRepository\u003c...\u003e{\n\n    // method 1\n    Page\u003c...\u003efindSomeTastyData(...);\n\n    // method 2 with using HQL\n    @Query(\"HQL to fetch data\")    \n    Page\u003c...\u003efindSomeTastyData(...);\n\n    // method 3: you could also use some custom count query \n    @Query(\n        value = \"HQL to fetch data\"\n        countQuery = \"HQL to count data\"\n    )    \n    Page\u003c...\u003efindSomeTastyData(...);\n\n}\n````\n\n# Problem\nLet's assume we have two entity as follows;\n- Book [see](./src/main/java/com/maemresen/springpaginationexample/entity/Book.java) \n- Chapter [see](./src/main/java/com/maemresen/springpaginationexample/entity/Chapter.java) \n\n**Where** one book having many chapters.\\\nSo there are `OneToMany` relation between `Book` and `Chapter` entities.\n\nNow, again, assume that we need to query book as following\nWe will have a query something as follows;\n\n````java\npublic interface BookRepository\u003cBook\u003e extends JpaRepository\u003cBook, Long\u003e {\n\n  @Query(\"SELECT b FROM Book b LEFT JOIN b.chapters WHERE ....\")\n  Page\u003cBook\u003e getBooks(Pageable pageable);\n}\n````\n\nBut what happens if we do something like this;\n\n````java\npublic interface BookRepository\u003cBook\u003e extends JpaRepository\u003cBook, Long\u003e {\n\n  @Query(\n      value = \"SELECT b FROM Book b LEFT JOIN b.chapters WHERE ....\",\n      countQuery = \"SELECT b FROM Book b\"\n  )\n  Page\u003cBook\u003e getBooks(Pageable pageable);\n}\n````\n\nAs you can see, query contains `LEFT JOIN` where count query not.\n\n# Testing\nAfter clone and build application, check the following API\n### API\n- **URI:** `/book`\n- **Params:**\n  - page: page number (index based, starts from 0)\n  - size: page size\n\nExample API call to get first page with size 1;\n```\nhttp://localhost:8080/book?page=0\u0026size=1\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaemresen%2Fspring-pagination-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaemresen%2Fspring-pagination-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaemresen%2Fspring-pagination-example/lists"}