hibernate/hibernate-orm · error · IllegalQueryOperationException
Cannot order by element " + element + " (there is no select
Error message
Cannot order by element " + element + " (there is no select list)
What it means
Thrown as IllegalQueryOperationException by SqmUtil.selectedNode when the order targets element 1, the query's select clause has no selection items (implicit selection, e.g. a bare 'from Person p' or a criteria query without .select()), and the Order either has no entity class (pure positional) or the query has more than one root. With an empty select list and no entity anchor, there is no select item the positional order can refer to, so Hibernate refuses it.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java:960
}
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)");
}
else {
return querySpec.getRootList().get(0);
}
}
else if ( element > items ) {
throw new IllegalQueryOperationException( "Cannot order by element " + element
+ " (there are only " + items + " select items)");
}
else {
return selectionItems.get( element - 1 );
}
}
public static boolean isSelectionAssignableToResultType(SqmSelection<?> selection, Class<?> expectedResultType) {
if ( expectedResultType == null ) {
return true;View on GitHub (pinned to fad1729dce)
Solutions
- Use an entity-anchored order for implicit selections: Order.asc(Person.class, "name")
- Add an explicit select clause so element positions exist: 'select p from Person p'
- Write the ordering into the HQL: 'from Person p order by p.name'
- For multi-root queries, select explicitly (multiselect) before using positional orders
Example fix
// before
List<Person> people = session.createSelectionQuery("from Person p", Person.class)
.addOrder(Order.by(1, SortDirection.ASCENDING))
.getResultList();
// after
List<Person> people = session.createSelectionQuery("from Person p order by p.name", Person.class)
.getResultList(); Defensive patterns
Strategy: validation
Validate before calling
static Order<?> safeOrderForImplicitSelection(Order<?> order, boolean hasExplicitSelect, int rootCount) {
if (!hasExplicitSelect && order.entityClass() == null && rootCount != 1) {
throw new IllegalArgumentException("Positional order needs an explicit select list");
}
return order;
} Try / catch
try {
query.addOrder(order).getResultList();
} catch (IllegalQueryOperationException e) {
// no select list to order by: fall back to entity-anchored ordering
query.addOrder(Order.asc(Person.class, "name")).getResultList();
} Prevention
- Add an explicit select clause when using positional orders
- Prefer entity-anchored orders for 'from Entity' style queries
- Ensure criteria queries set .select() before element-based ordering
When it happens
Trigger: Order.by(1, direction) applied to a query with an empty select clause and no entity anchor (e.g. session.createQuery("from Person p").addOrder(Order.by(1, ...)) in a state where selection items are not populated); a criteria query with no .select() and two .from(...) roots combined with a positional order; order specs deserialized without an entityClass field but with element=1.
Common situations: Generic pagination layers that add Order.by(1) as a deterministic tiebreaker regardless of query shape; criteria built dynamically where select() was skipped and a second root was added; mixing positional and entity-anchored order styles.
Related errors
- Select item was of wrong entity type
- Select item was not an entity type
- Query has multiple items in the select list
- Cannot order by element " + element + " (the first select it
- Cannot order by element " + element + " (there are only " +
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/948f22a4aa491526.
Report an issue: GitHub.