{"record":{"id":"e9cf2fb276f16991","repo":"quarkusio/quarkus","slug":"cannot-call-a-page-related-method-in-a-ranged-quer-e9cf2f","errorCode":null,"errorMessage":"Cannot call a page related method in a ranged query, call page(Page) or page(int, int) to initiate pagination first","messagePattern":"Cannot call a page related method in a ranged query, call page\\(Page\\) or page\\(int, int\\) to initiate pagination first","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java","lineNumber":136,"sourceCode":"        long count = count();\n        if (count == 0)\n            return 1; // a single page of zero results\n        return (int) Math.ceil((double) count / (double) page.size);\n    }\n\n    public Page page() {\n        checkPagination();\n        return page;\n    }\n\n    private void checkPagination() {\n        if (page == null) {\n            throw new UnsupportedOperationException(\n                    \"Cannot call a page related method, \"\n                            + \"call page(Page) or page(int, int) to initiate pagination first\");\n        }\n        if (range != null) {\n            throw new UnsupportedOperationException(\"Cannot call a page related method in a ranged query, \" +\n                    \"call page(Page) or page(int, int) to initiate pagination first\");\n        }\n    }\n\n    public <T extends Entity> CommonPanacheQueryImpl<T> range(int startIndex, int lastIndex) {\n        this.range = Range.of(startIndex, lastIndex);\n        // reset the page to its default to be able to switch from page to range\n        this.page = null;\n        return (CommonPanacheQueryImpl<T>) this;\n    }\n\n    public <T extends Entity> CommonPanacheQueryImpl<T> withCollation(Collation collation) {\n        this.collation = collation;\n        return (CommonPanacheQueryImpl<T>) this;\n    }\n\n    public <T extends Entity> CommonPanacheQueryImpl<T> withReadPreference(ReadPreference readPreference) {\n        this.collection = this.collection.withReadPreference(readPreference);","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java#L118-L154","documentation":"A query that was given an explicit range via range(startIndex, lastIndex) cannot use page navigation methods; Panache models paging and ranging as mutually exclusive slice modes. checkPagination() throws this UnsupportedOperationException when range is set, telling you to switch to page-based pagination first.","triggerScenarios":"Calling range(a,b) on a PanacheQuery and then invoking nextPage(), previousPage(), firstPage(), lastPage(), hasNextPage(), or hasPreviousPage() on it.","commonSituations":"Code that mixes windowed fetching (range) with later pagination logic; refactors where a previously paged query was switched to range for a one-off slice but downstream paging calls were left in place; utility helpers that always call hasNextPage after fetching.","solutions":["Replace range(...) with page(pageIndex, pageSize) (or page(Page.of(...))) before calling page-related methods","Remove the page-related method calls when using range-based slicing; compute boundaries manually","Restructure the code so a query uses either pagination or ranging, not both"],"exampleFix":"// before\nPanacheQuery<Person> q = Person.findAll().range(0, 19);\nboolean more = q.hasNextPage();\n\n// after\nPanacheQuery<Person> q = Person.findAll().page(0, 20);\nboolean more = q.hasNextPage();","handlingStrategy":"validation","validationCode":"if (query.range() != null) {\n    throw new IllegalStateException(\"Use page(...) instead of range(...) with page-related methods\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    boolean more = query.hasNextPage();\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage().contains(\"ranged query\")) {\n        throw new IllegalStateException(\"Switch this query from range(...) to page(...) to use paging\", e);\n    }\n    throw e;\n}","preventionTips":["Pick one slicing mode per query: either range(start,last) or page(i,size), never both","Audit helpers that call hasNextPage()/nextPage() so they only accept paged queries","Document in repository interfaces whether methods return paged or ranged queries","Prefer page-based APIs when the caller needs navigation; reserve range for fixed windows"],"tags":["mongodb","panache","pagination","range","unsupported-operation"],"backgroundTag":"pagination-not-initialized","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}