{"record":{"id":"76687ce61e40380d","repo":"hibernate/hibernate-orm","slug":"select-item-was-not-an-entity-type-76687c","errorCode":null,"errorMessage":"Select item was not an entity type","messagePattern":"Select item was not an entity type","errorType":"exception","errorClass":"IllegalQueryOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java","lineNumber":940,"sourceCode":"\t\t\t\t\torder.direction(), order.nullPrecedence(), !order.caseSensitive()\n\t\t\t);\n\t\t}\n\t\telse {\n\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 ) {","sourceCodeStart":922,"sourceCodeEnd":958,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java#L922-L958","documentation":"Thrown as IllegalQueryOperationException when an entity-anchored Order (Order.asc(Class, attributeName)) is applied to a query whose single select item is not an entity root — it is a scalar or path expression (e.g. p.name, a count, a literal). Ordering by 'attribute of the returned entity' only works when the select item is the entity itself (an SqmFrom); for scalar projections there is no entity to resolve attributeName against.","triggerScenarios":"session.createQuery(\"select p.name from Person p\", String.class).addOrder(Order.asc(Person.class, \"age\")); projection queries (DTO constructors, count queries, single-column selects) combined with entity-attribute ordering; report queries that select scalars but reuse the ordering helper written for entity queries.","commonSituations":"A shared ordering utility that always builds Order.by(entityClass, attribute) is reused on projection queries; refactoring an entity query into a DTO projection without touching the order clause.","solutions":["Move the sort into the HQL: 'select p.name from Person p order by p.age'","Order by select-list element position: Order.by(1, SortDirection.ASCENDING)","Select the entity itself if you need entity-attribute ordering, then map to the DTO in Java"],"exampleFix":"// before\nList<String> names = session.createSelectionQuery(\"select p.name from Person p\", String.class)\n        .addOrder(Order.asc(Person.class, \"age\"))\n        .getResultList();\n// after\nList<String> names = session.createSelectionQuery(\"select p.name from Person p order by p.age\", String.class)\n        .getResultList();","handlingStrategy":"validation","validationCode":"static boolean orderUsableOnProjection(Order<?> order, boolean selectItemIsEntity) {\n    return order.entityClass() == null || selectItemIsEntity;\n}\n// for projection queries (select p.name), reject entity-anchored orders up front:\nif (order.entityClass() != null && !selectItemIsEntity) {\n    order = Order.by(1, SortDirection.ASCENDING);\n}","typeGuard":"static boolean isEntitySelection(org.hibernate.query.sqm.tree.select.SqmSelectableNode<?> node) {\n    return node instanceof org.hibernate.query.sqm.tree.from.SqmFrom<?, ?>;\n}","tryCatchPattern":"try {\n    query.addOrder(order).getResultList();\n} catch (IllegalQueryOperationException e) {\n    // projection cannot be ordered by entity attribute: rewrite as order-by element or inline SQL\n    throw new IllegalArgumentException(\"Use element-based ordering for projection queries\", e);\n}","preventionTips":["Do not reuse entity-anchored ordering helpers on scalar/DTO projections","Put order-by into the HQL for projection queries","Check that the select item is the entity itself before attaching Order.asc(Class, attribute)"],"tags":["hibernate","order-by","projection","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"}