{"record":{"id":"2c0829e903ca0ed6","repo":"hibernate/hibernate-orm","slug":"select-item-was-of-wrong-entity-type","errorCode":null,"errorMessage":"Select item was of wrong entity type","messagePattern":"Select item was of wrong entity type","errorType":"exception","errorClass":"IllegalQueryOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/internal/KeyBasedPagination.java","lineNumber":104,"sourceCode":"\t\t\tfinal List<SqmPath<?>> previousKeys = keyPaths.subList(0, i);\n\t\t\tfinal SqmPredicate predicate = keyPredicate( key, keyValue, direction, previousKeys, keyValues, builder );\n\t\t\trestriction = restriction == null ? predicate : builder.or( restriction, predicate );\n\t\t}\n\t\treturn restriction;\n\t}\n\n\tprivate static <R> JpaCompoundSelection<KeyedResult<R>> keySelection(\n\t\t\tList<Order<? super R>> keyDefinition,\n\t\t\tSqmFrom<?, ?> root, JpaSelection<?> selected,\n\t\t\tNodeBuilder builder) {\n\t\tfinal List<SqmPath<?>> items = new ArrayList<>();\n\t\tfor ( Order<? super R> key : keyDefinition ) {\n\t\t\tif ( key.entityClass() == null ) {\n\t\t\t\tthrow new IllegalQueryOperationException(\"Key-based pagination based on select list items is not yet supported\");\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif ( !key.entityClass().isAssignableFrom( selected.getJavaType() ) ) {\n\t\t\t\t\tthrow new IllegalQueryOperationException(\"Select item was of wrong entity type\");\n\t\t\t\t}\n\t\t\t\t// ordering by an attribute of the returned entity\n\t\t\t\titems.add( root.get( key.attributeName() ) );\n\t\t\t}\n\t\t}\n\t\treturn keyedResultConstructor( selected, builder, items );\n\t}\n\n\tprivate static <R> JpaCompoundSelection<KeyedResult<R>> keyedResultConstructor(\n\t\t\tJpaSelection<?> selected, NodeBuilder builder, List<SqmPath<?>> newItems) {\n\t\t@SuppressWarnings({\"rawtypes\", \"unchecked\"})\n\t\tfinal Class<KeyedResult<R>> resultClass = (Class) KeyedResult.class;\n\t\treturn builder.construct( resultClass, asList( selected, builder.construct(List.class, newItems ) ) );\n\t}\n\n\t@SuppressWarnings(\"rawtypes\")\n\tprivate static <C extends Comparable<? super C>> SqmPredicate keyPredicate(\n\t\t\tExpression<? extends C> key, C keyValue, SortDirection direction,","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/internal/KeyBasedPagination.java#L86-L122","documentation":"Error \"Select item was of wrong entity type\" thrown in hibernate/hibernate-orm.","triggerScenarios":"The query structure does not meet the single-root/single-select-item requirement: Select item was of wrong entity type","commonSituations":"Key-based pagination or Specifications applied to queries with zero/multiple roots or multi-item select lists.","solutions":["The selected entity type does not match the expected type for key-based pagination. Select the entity declared by the query root."],"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"}