hibernate/hibernate-orm · error · IllegalQueryOperationException
Select item was of wrong entity type
Error message
Select item was of wrong entity type
What it means
Error "Select item was of wrong entity type" thrown in hibernate/hibernate-orm.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/query/internal/KeyBasedPagination.java:104
final List<SqmPath<?>> previousKeys = keyPaths.subList(0, i);
final SqmPredicate predicate = keyPredicate( key, keyValue, direction, previousKeys, keyValues, builder );
restriction = restriction == null ? predicate : builder.or( restriction, predicate );
}
return restriction;
}
private static <R> JpaCompoundSelection<KeyedResult<R>> keySelection(
List<Order<? super R>> keyDefinition,
SqmFrom<?, ?> root, JpaSelection<?> selected,
NodeBuilder builder) {
final List<SqmPath<?>> items = new ArrayList<>();
for ( Order<? super R> key : keyDefinition ) {
if ( key.entityClass() == null ) {
throw new IllegalQueryOperationException("Key-based pagination based on select list items is not yet supported");
}
else {
if ( !key.entityClass().isAssignableFrom( selected.getJavaType() ) ) {
throw new IllegalQueryOperationException("Select item was of wrong entity type");
}
// ordering by an attribute of the returned entity
items.add( root.get( key.attributeName() ) );
}
}
return keyedResultConstructor( selected, builder, items );
}
private static <R> JpaCompoundSelection<KeyedResult<R>> keyedResultConstructor(
JpaSelection<?> selected, NodeBuilder builder, List<SqmPath<?>> newItems) {
@SuppressWarnings({"rawtypes", "unchecked"})
final Class<KeyedResult<R>> resultClass = (Class) KeyedResult.class;
return builder.construct( resultClass, asList( selected, builder.construct(List.class, newItems ) ) );
}
@SuppressWarnings("rawtypes")
private static <C extends Comparable<? super C>> SqmPredicate keyPredicate(
Expression<? extends C> key, C keyValue, SortDirection direction,View on GitHub (pinned to fad1729dce)
Solutions
- The selected entity type does not match the expected type for key-based pagination. Select the entity declared by the query root.
When it happens
Trigger: The query structure does not meet the single-root/single-select-item requirement: Select item was of wrong entity type
Common situations: Key-based pagination or Specifications applied to queries with zero/multiple roots or multi-item select lists.
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/2c0829e903ca0ed6.
Report an issue: GitHub.