hibernate/hibernate-orm · error · IllegalQueryOperationException
Query has multiple items in the select list
Error message
Query has multiple items in the select list
What it means
Thrown as IllegalQueryOperationException when an entity-anchored Order (Order.asc(Class, attributeName)) is applied to a query whose select list has more than one item. Resolving 'attributeName' against 'the returned entity' is only defined when there is exactly one select item; with multiple items Hibernate cannot know which item's entity the attribute belongs to, so it rejects the order.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java:944
// ordering by an attribute of the returned entity
if ( items.size() <= 1) {
if ( selected instanceof SqmFrom<?, ?> root ) {
if ( !order.entityClass().isAssignableFrom( root.getJavaType() ) ) {
throw new IllegalQueryOperationException("Select item was of wrong entity type");
}
final StringTokenizer tokens = new StringTokenizer( order.attributeName(), "." );
SqmPath<?> path = root;
while ( tokens.hasMoreTokens() ) {
path = path.get( tokens.nextToken() );
}
return builder.sort( path, order.direction(), order.nullPrecedence(), !order.caseSensitive() );
}
else {
throw new IllegalQueryOperationException("Select item was not an entity type");
}
}
else {
throw new IllegalQueryOperationException("Query has multiple items in the select list");
}
}
}
private static SqmSelectableNode<?> selectedNode(AbstractSqmSelectQuery<?> sqm, Order<?> order) {
final int element = order.element();
if ( element < 1) {
throw new IllegalQueryOperationException("Cannot order by element " + element
+ " (the first select item is element 1)");
}
final var querySpec = sqm.getQuerySpec();
final var selectionItems = querySpec.getSelectClause().getSelectionItems();
final int items = selectionItems.size();
if ( items == 0 && element == 1 ) {
if ( order.entityClass() == null || querySpec.getRootList().size() > 1 ) {
throw new IllegalQueryOperationException("Cannot order by element " + element
+ " (there is no select list)");
}View on GitHub (pinned to fad1729dce)
Solutions
- Order by select-list element position: Order.by(2, SortDirection.ASCENDING)
- Put the ordering into the HQL: 'select p.name, p.age from Person p order by p.age'
- Select a single entity or DTO-construct expression if you want entity-attribute ordering
Example fix
// before
List<Object[]> rows = session.createSelectionQuery("select p.name, p.age from Person p", Object[].class)
.addOrder(Order.asc(Person.class, "age"))
.getResultList();
// after
List<Object[]> rows = session.createSelectionQuery("select p.name, p.age from Person p order by p.age", Object[].class)
.getResultList(); Defensive patterns
Strategy: validation
Validate before calling
static Order<?> positionalOrderFor(Order<?> order, int selectItemCount) {
if (order.entityClass() != null && selectItemCount > 1) {
return Order.by(1, SortDirection.ASCENDING); // or map attribute -> element index
}
return order;
}
// call before addOrder: query.addOrder(positionalOrderFor(order, selectItemCount)); Try / catch
try {
query.addOrder(order).getResultList();
} catch (IllegalQueryOperationException e) {
// multi-select: switch to element-based ordering
query.addOrder(Order.by(1, SortDirection.ASCENDING)).getResultList();
} Prevention
- For multi-select queries always use element positions or inline order-by
- Keep a per-query mapping from sort field to select element index
- Re-check ordering code whenever a select list gains or loses items
When it happens
Trigger: session.createQuery("select p.name, p.age from Person p").addOrder(Order.asc(Person.class, "age")); multi-select tuple/DTO queries ('select p.name, a.city from Person p join p.address a') combined with Order.asc(Class, String); grid components that attach entity-attribute ordering to arbitrary custom queries.
Common situations: A generic sorting layer attaches Order.by(entityClass, attribute) to user-supplied queries that happen to be multi-select; evolving a query from one select item to several without revisiting the ordering code.
Related errors
- Select item was of wrong entity type
- Select item was not an entity type
- Cannot order by element " + element + " (the first select it
- Cannot order by element " + element + " (there is no select
- Cannot order by element " + element + " (there are only " +
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/f529da268b0dbf26.
Report an issue: GitHub.