{"record":{"id":"948f22a4aa491526","repo":"hibernate/hibernate-orm","slug":"cannot-order-by-element-element-there-is","errorCode":null,"errorMessage":"Cannot order by element \" + element + \" (there is no select list)","messagePattern":"Cannot order by element \" \\+ element \\+ \" \\(there is no select list\\)","errorType":"exception","errorClass":"IllegalQueryOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java","lineNumber":960,"sourceCode":"\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new IllegalQueryOperationException(\"Query has multiple items in the select list\");\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static SqmSelectableNode<?> selectedNode(AbstractSqmSelectQuery<?> sqm, Order<?> order) {\n\t\tfinal int element = order.element();\n\t\tif ( element < 1) {\n\t\t\tthrow new IllegalQueryOperationException(\"Cannot order by element \" + element\n\t\t\t\t\t+ \" (the first select item is element 1)\");\n\t\t}\n\t\tfinal var querySpec = sqm.getQuerySpec();\n\t\tfinal var selectionItems = querySpec.getSelectClause().getSelectionItems();\n\t\tfinal int items = selectionItems.size();\n\t\tif ( items == 0 && element == 1 ) {\n\t\t\tif ( order.entityClass() == null || querySpec.getRootList().size() > 1 ) {\n\t\t\t\tthrow new IllegalQueryOperationException(\"Cannot order by element \" + element\n\t\t\t\t\t\t+ \" (there is no select list)\");\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn querySpec.getRootList().get(0);\n\t\t\t}\n\t\t}\n\t\telse if ( element > items ) {\n\t\t\tthrow new IllegalQueryOperationException( \"Cannot order by element \" + element\n\t\t\t\t\t+ \" (there are only \" + items + \" select items)\");\n\t\t}\n\t\telse {\n\t\t\treturn selectionItems.get( element - 1 );\n\t\t}\n\t}\n\n\tpublic static boolean isSelectionAssignableToResultType(SqmSelection<?> selection, Class<?> expectedResultType) {\n\t\tif ( expectedResultType == null ) {\n\t\t\treturn true;","sourceCodeStart":942,"sourceCodeEnd":978,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java#L942-L978","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nList<Person> people = session.createSelectionQuery(\"from Person p\", Person.class)\n        .addOrder(Order.by(1, SortDirection.ASCENDING))\n        .getResultList();\n// after\nList<Person> people = session.createSelectionQuery(\"from Person p order by p.name\", Person.class)\n        .getResultList();","handlingStrategy":"validation","validationCode":"static Order<?> safeOrderForImplicitSelection(Order<?> order, boolean hasExplicitSelect, int rootCount) {\n    if (!hasExplicitSelect && order.entityClass() == null && rootCount != 1) {\n        throw new IllegalArgumentException(\"Positional order needs an explicit select list\");\n    }\n    return order;\n}","typeGuard":null,"tryCatchPattern":"try {\n    query.addOrder(order).getResultList();\n} catch (IllegalQueryOperationException e) {\n    // no select list to order by: fall back to entity-anchored ordering\n    query.addOrder(Order.asc(Person.class, \"name\")).getResultList();\n}","preventionTips":["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"],"tags":["hibernate","order-by","select-list","query-api"],"backgroundTag":"invalid-order-by-clause","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}