{"record":{"id":"b692ce4de3fee9e2","repo":"hibernate/hibernate-orm","slug":"query-did-not-define-any-query-roots","errorCode":null,"errorMessage":"Query did not define any query roots","messagePattern":"Query did not define any query roots","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/internal/KeyBasedPagination.java","lineNumber":44,"sourceCode":"import static org.hibernate.query.sqm.internal.SqmUtil.sortSpecification;\n\n/**\n * Manipulation of SQM query tree for key-based pagination.\n *\n * @author Gavin King\n */\npublic class KeyBasedPagination {\n\n\tpublic static <R> SqmSelectStatement<KeyedResult<R>> paginate(\n\t\t\tList<Order<? super R>> keyDefinition, List<Comparable<?>> keyValues,\n\t\t\tSqmSelectStatement<KeyedResult<R>> statement, NodeBuilder builder) {\n\t\tfinal SqmQuerySpec<?> querySpec = statement.getQuerySpec();\n\t\tfinal List<? extends JpaSelection<?>> items = querySpec.getSelectClause().getSelectionItems();\n\t\tfinal JpaSelection<?> selected = switch ( items.size() ) {\n\t\t\tcase 0 -> {\n\t\t\t\tfinal List<SqmRoot<?>> sqmRoots = querySpec.getRootList();\n\t\t\t\tif ( sqmRoots == null || sqmRoots.isEmpty() ) {\n\t\t\t\t\tthrow new IllegalArgumentException( \"Query did not define any query roots\" );\n\t\t\t\t}\n\t\t\t\tif ( sqmRoots.size() != 1 ) {\n\t\t\t\t\tthrow new IllegalArgumentException( \"Query has multiple query roots\" );\n\t\t\t\t}\n\t\t\t\tyield sqmRoots.get( 0 );\n\t\t\t}\n\t\t\tcase 1 -> items.get( 0 );\n\t\t\tdefault -> throw new IllegalQueryOperationException(\"Query has multiple items in the select list\");\n\t\t};\n\t\tif ( selected instanceof SqmFrom<?, ?> root ) {\n\t\t\tstatement.orderBy( keyDefinition.stream().map( order -> sortSpecification( statement, order ) )\n\t\t\t\t\t.collect( toList() ) );\n\t\t\tstatement.select( keySelection( keyDefinition, root, selected, builder ) );\n\t\t\tif ( keyValues != null ) {\n\t\t\t\tfinal SqmPredicate restriction = keyRestriction( keyDefinition, keyValues, root, builder );\n\t\t\t\tfinal SqmPredicate queryWhere = querySpec.getRestriction();\n\t\t\t\tstatement.where( queryWhere == null ? restriction : builder.and( queryWhere, restriction ) );\n\t\t\t}","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/internal/KeyBasedPagination.java#L26-L62","documentation":"Error \"Query did not define any query roots\" thrown in hibernate/hibernate-orm.","triggerScenarios":"A runtime precondition of the Hibernate query API is violated: Query did not define any query roots","commonSituations":"Application code or configuration calls the query API in a way that violates its documented contract; the interpolated values in the message identify the offending input.","solutions":["Key-based pagination requires a query with exactly one root. Add a from-clause root to the query."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}