{"record":{"id":"f527d7e974d283e7","repo":"hibernate/hibernate-orm","slug":"query-has-multiple-items-in-the-select-list","errorCode":null,"errorMessage":"Query has multiple items in the select list","messagePattern":"Query has multiple items in the select list","errorType":"exception","errorClass":"IllegalQueryOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/internal/KeyBasedPagination.java","lineNumber":52,"sourceCode":"\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}\n\t\t\treturn statement;\n\t\t}\n\t\telse {\n\t\t\tthrow new IllegalQueryOperationException(\"Select item was not an entity type\");\n\t\t}\n\t}\n\n\t@SuppressWarnings({\"rawtypes\", \"unchecked\"})","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/internal/KeyBasedPagination.java#L34-L70","documentation":"Error \"Query has multiple items in the select list\" thrown in hibernate/hibernate-orm.","triggerScenarios":"The query structure does not meet the single-root/single-select-item requirement: Query has multiple items in the select list","commonSituations":"Key-based pagination or Specifications applied to queries with zero/multiple roots or multi-item select lists.","solutions":["Key-based pagination requires a single select item. Select only the paginated entity."],"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"}