{"record":{"id":"ea202893174ac68e","repo":"quarkusio/quarkus","slug":"cannot-call-haspreviouspage-before-fetching-resu","errorCode":null,"errorMessage":"Cannot call hasPreviousPage() before fetching results with list()","messagePattern":"Cannot call hasPreviousPage\\(\\) 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":263,"sourceCode":"    }\n\n    public boolean hasNextPage() {\n        checkPagination();\n        if (keyedPage != null) {\n            if (lastKeyedResult == null) {\n                throw new UnsupportedOperationException(\n                        \"Cannot call hasNextPage() before fetching results with list()\");\n            }\n            return !lastKeyedResult.isLastPage();\n        }\n        return page.index < (pageCount() - 1);\n    }\n\n    public boolean hasPreviousPage() {\n        checkPagination();\n        if (keyedPage != null) {\n            if (lastKeyedResult == null) {\n                throw new UnsupportedOperationException(\n                        \"Cannot call hasPreviousPage() before fetching results with list()\");\n            }\n            return !lastKeyedResult.isFirstPage();\n        }\n        return page.index > 0;\n    }\n\n    public int pageCount() {\n        checkPagination();\n        long count = count();\n        if (count == 0)\n            return 1; // a single page of zero results\n        int pageSize = keyedPage != null ? keyedPage.getPage().getSize() : page.size;\n        return (int) Math.ceil((double) count / (double) pageSize);\n    }\n\n    public Page page() {\n        checkPagination();","sourceCodeStart":245,"sourceCodeEnd":281,"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#L245-L281","documentation":"With cursor-based (keyed) pagination, hasPreviousPage() relies on the cursor state of the previously fetched result. If no page has been fetched via list() yet, Panache throws UnsupportedOperationException because there is no cursor indicating whether the current page is the first one.","triggerScenarios":"Calling hasPreviousPage() on a query paginated with KeyedPage before calling list()/stream() at least once.","commonSituations":"Building UI pagination controls (prev/next flags) before executing the query, a pattern that works with offset pagination but not with keyed pagination.","solutions":["Call list() first, then call hasPreviousPage().","Use offset pagination (page(int, int)) if you need pre-fetch page flags.","Track the first page yourself: keyed pagination started at the beginning implies no previous page until you have navigated."],"exampleFix":"// before\nquery.page(KeyedPage.of(KeyedPage.PREVIOUS, 50));\nboolean prev = query.hasPreviousPage(); // throws\n\n// after\nquery.page(KeyedPage.of(KeyedPage.PREVIOUS, 50));\nList<MyEntity> page = query.list();\nboolean prev = query.hasPreviousPage();","handlingStrategy":"validation","validationCode":"if (lastFetchedPage == null) {\n    query.list(); // fetch current page first\n}\nboolean hasPrev = query.hasPreviousPage();","typeGuard":null,"tryCatchPattern":"try {\n    return query.hasPreviousPage();\n} catch (UnsupportedOperationException e) {\n    query.list();\n    return query.hasPreviousPage();\n}","preventionTips":["Fetch the page before asking for prev/next flags under keyed pagination.","Track navigation state (started at first page => no previous) in your own code.","Keep pagination flag checks after the fetch call in a single utility method."],"tags":["panache","pagination","cursor","call-order"],"backgroundTag":"cursor-pagination-fetch-before-inspect","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}