{"record":{"id":"f2749728840e7a9e","repo":"hibernate/hibernate-orm","slug":"selectquery-has-no-selection-items","errorCode":null,"errorMessage":"selectQuery has no selection items","messagePattern":"selectQuery has no selection items","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tuple/internal/AnonymousTupleType.java","lineNumber":66,"sourceCode":" * @author Christian Beikov\n */\n@Incubating\npublic class AnonymousTupleType<T>\n\t\timplements TupleType<T>, SqmDomainType<T>, SqmPathSource<T> {\n\n\tprivate final JavaType<T> javaTypeDescriptor;\n\tprivate final @Nullable NavigablePath[] componentSourcePaths;\n\tprivate final SqmBindableType<?>[] expressibles;\n\tprivate final String[] componentNames;\n\tprivate final Map<String, Integer> componentIndexMap;\n\n\tpublic AnonymousTupleType(SqmSelectQuery<T> selectQuery) {\n\t\tfinal SqmSelectClause selectClause = selectQuery.getQueryPart()\n\t\t\t\t.getFirstQuerySpec()\n\t\t\t\t.getSelectClause();\n\n\t\tif ( selectClause.getSelections().isEmpty() ) {\n\t\t\tthrow new IllegalArgumentException( \"selectQuery has no selection items\" );\n\t\t}\n\t\t// todo: right now, we \"snapshot\" the state of the selectQuery when creating this type, but maybe we shouldn't?\n\t\t//  i.e. what if the selectQuery changes later on? Or should we somehow mark the selectQuery to signal,\n\t\t//  that changes to the select clause are invalid after a certain point?\n\n\t\tfinal List<SqmSelection<?>> selections = selectClause.getSelections();\n\t\tfinal List<SqmSelectableNode<?>> selectableNodes = new ArrayList<>();\n\t\tfinal List<String> aliases = new ArrayList<>();\n\t\tfor ( SqmSelection<?> selection : selections ) {\n\t\t\tfinal boolean compound = selection.getSelectableNode().isCompoundSelection();\n\t\t\tselection.getSelectableNode().visitSubSelectableNodes( node -> {\n\t\t\t\tselectableNodes.add( node );\n\t\t\t\tif ( compound ) {\n\t\t\t\t\taliases.add( node.getAlias() );\n\t\t\t\t}\n\t\t\t} );\n\t\t\tif ( !compound ) {\n\t\t\t\t// for compound selections we use the sub-selectable nodes aliases","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tuple/internal/AnonymousTupleType.java#L48-L84","documentation":"AnonymousTupleType(SqmSelectQuery) snapshots the first QuerySpec's select clause of a CTE or from-clause subquery to derive the tuple's component names and types. An empty selection list cannot describe a tuple, so the constructor throws IllegalArgumentException('selectQuery has no selection items') while Hibernate compiles the query.","triggerScenarios":"Building a tuple type from an SqmSelectQuery whose first query spec has zero selections: criteria/SQM subqueries where select(...) was never called or the selection list was cleared, or HQL that ends up with an empty select clause before translation.","commonSituations":"Dynamic criteria/HQL string building that drops the select list under some branch; programmatic SQM construction in framework or test code; migration where implicit selection behavior changed between Hibernate versions.","solutions":["Make sure the subquery/CTE projects at least one item: call sub.select(...) (criteria) or add explicit select items (HQL) before compiling.","Validate dynamically built queries for a non-empty select list before handing them to Hibernate.","Log the generated HQL/SQM at query-creation time so empty select clauses are visible in tests."],"exampleFix":"// before - subquery never selects anything\ncq.subquery(Object[].class); // no select() call -> IllegalArgumentException at compile\n\n// after - always define selections\nSubquery<Object[]> sub = cq.subquery(Object[].class);\nRoot<Entity> e = sub.from(Entity.class);\nsub.select(cb.array(e.get(\"id\"), e.get(\"name\")));","handlingStrategy":"validation","validationCode":"// guard in dynamic query builders: require a selection before compiling\nif (subquerySelectClauseIsEmpty(sq)) {\n    throw new IllegalArgumentException(\"CTE/subquery must project at least one item\");\n}\nsession.createQuery(hql); // only compile after the guard","typeGuard":null,"tryCatchPattern":"try {\n    return session.createQuery(hql, Object[].class).getResultList();\n} catch (IllegalArgumentException e) {\n    // 'selectQuery has no selection items': fix query construction\n    throw new QueryBuildException(\"Generated query has an empty select list: \" + hql, e);\n}","preventionTips":["Always call select(...) on criteria subqueries immediately after from(...).","Unit-test query builders for all branches so none can emit an empty select list.","Log generated HQL/SQM at creation time to catch empty select clauses early."],"tags":["hibernate","criteria-api","sqm","validation"],"backgroundTag":"empty-select-clause","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}