{"record":{"id":"6392aa471403034b","repo":"quarkusio/quarkus","slug":"cannot-call-nextpage-before-fetching-results-wit","errorCode":null,"errorMessage":"Cannot call nextPage() before fetching results with list()","messagePattern":"Cannot call nextPage\\(\\) before fetching results with list\\(\\)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java","lineNumber":200,"sourceCode":"\n    @SuppressWarnings(\"unchecked\")\n    public void cursor(int pageIndex, int pageSize) {\n        if (sort == null || sort.getColumns().isEmpty()) {\n            throw new UnsupportedOperationException(\n                    \"Cannot use cursor-based pagination without sort criteria: use find(entityClass, query, sort) or findAll(entityClass, sort)\");\n        }\n        List orders = PanacheJpaUtil.toHibernateOrders(entityClass, sort);\n        this.keyedPage = org.hibernate.query.Page.page(pageSize, pageIndex).keyedBy(orders);\n        this.lastKeyedResult = null;\n        this.page = null;\n        this.range = null;\n    }\n\n    public void nextPage() {\n        checkPagination();\n        if (keyedPage != null) {\n            if (lastKeyedResult == null) {\n                throw new UnsupportedOperationException(\n                        \"Cannot call nextPage() before fetching results with list()\");\n            }\n            keyedPage = lastKeyedResult.getNextPage();\n            lastKeyedResult = null;\n        } else {\n            page(page.next());\n        }\n    }\n\n    public void previousPage() {\n        checkPagination();\n        if (keyedPage != null) {\n            if (lastKeyedResult == null) {\n                throw new UnsupportedOperationException(\n                        \"Cannot call previousPage() before fetching results with list()\");\n            }\n            KeyedPage<?> prev = lastKeyedResult.getPreviousPage();\n            if (prev != null) {","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java#L182-L218","documentation":"In keyed (cursor) pagination, next-page keys come from the last fetched KeyedResult. Calling nextPage() before any results were fetched via list() means there is no key to advance from, so Panache throws UnsupportedOperationException.","triggerScenarios":"Calling query.cursor(...).nextPage() immediately after pagination setup, before .list() has produced lastKeyedResult.","commonSituations":"Iterating pages in a loop where nextPage() is called at loop start instead of after fetching; misunderstanding the cursor API order (list first, then advance).","solutions":["Call .list() (or .firstResult()) first, then nextPage()","Reorder the loop: fetch results, process, then call nextPage() for the next iteration","Use offset pagination (page()) if you don't need keyset semantics, since page.next() works without a prior fetch"],"exampleFix":"// before\nquery.cursor(0, 20).nextPage();\n// after\nList<Person> results = query.cursor(0, 20).list();\nquery.nextPage();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    query.nextPage();\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"before fetching results\")) {\n        query.list(); // fetch first, then advance\n        query.nextPage();\n    } else throw e;\n}","preventionTips":["Always call list() before nextPage() on a keyed query","Structure page loops as: fetch, process, advance","Track cursor state explicitly when paginating across requests"],"tags":["quarkus","panache","pagination","cursor"],"backgroundTag":"nextpage-before-fetch","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"}