{"record":{"id":"f529da268b0dbf26","repo":"hibernate/hibernate-orm","slug":"query-has-multiple-items-in-the-select-list-f529da","errorCode":null,"errorMessage":"Query has multiple items in the select list","messagePattern":"Query has multiple items in the select list","errorType":"exception","errorClass":"IllegalQueryOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java","lineNumber":944,"sourceCode":"\t\t\t// ordering by an attribute of the returned entity\n\t\t\tif ( items.size() <= 1) {\n\t\t\t\tif ( selected instanceof SqmFrom<?, ?> root ) {\n\t\t\t\t\tif ( !order.entityClass().isAssignableFrom( root.getJavaType() ) ) {\n\t\t\t\t\t\tthrow new IllegalQueryOperationException(\"Select item was of wrong entity type\");\n\t\t\t\t\t}\n\t\t\t\t\tfinal StringTokenizer tokens = new StringTokenizer( order.attributeName(), \".\" );\n\t\t\t\t\tSqmPath<?> path = root;\n\t\t\t\t\twhile ( tokens.hasMoreTokens() ) {\n\t\t\t\t\t\tpath = path.get( tokens.nextToken() );\n\t\t\t\t\t}\n\t\t\t\t\treturn builder.sort( path, order.direction(), order.nullPrecedence(), !order.caseSensitive() );\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthrow new IllegalQueryOperationException(\"Select item was not an entity type\");\n\t\t\t\t}\n\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}","sourceCodeStart":926,"sourceCodeEnd":962,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java#L926-L962","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nList<Object[]> rows = session.createSelectionQuery(\"select p.name, p.age from Person p\", Object[].class)\n        .addOrder(Order.asc(Person.class, \"age\"))\n        .getResultList();\n// after\nList<Object[]> rows = session.createSelectionQuery(\"select p.name, p.age from Person p order by p.age\", Object[].class)\n        .getResultList();","handlingStrategy":"validation","validationCode":"static Order<?> positionalOrderFor(Order<?> order, int selectItemCount) {\n    if (order.entityClass() != null && selectItemCount > 1) {\n        return Order.by(1, SortDirection.ASCENDING); // or map attribute -> element index\n    }\n    return order;\n}\n// call before addOrder: query.addOrder(positionalOrderFor(order, selectItemCount));","typeGuard":null,"tryCatchPattern":"try {\n    query.addOrder(order).getResultList();\n} catch (IllegalQueryOperationException e) {\n    // multi-select: switch to element-based ordering\n    query.addOrder(Order.by(1, SortDirection.ASCENDING)).getResultList();\n}","preventionTips":["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"],"tags":["hibernate","order-by","multi-select","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"}