{"record":{"id":"06b6a0cf333e39a4","repo":"hibernate/hibernate-orm","slug":"composite-query-parameter-cannot-be-used-in-select","errorCode":null,"errorMessage":"Composite query parameter cannot be used in select","messagePattern":"Composite query parameter cannot be used in select","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/sql/internal/SqmParameterInterpretation.java","lineNumber":87,"sourceCode":"\t}\n\n\t@Override\n\tpublic void accept(SqlAstWalker sqlTreeWalker) {\n\t\tgetResolvedExpression().accept( sqlTreeWalker );\n\t}\n\n\t@Override\n\tpublic MappingModelExpressible<?> getExpressionType() {\n\t\treturn valueMapping;\n\t}\n\n\t@Override\n\tpublic DomainResult<?> createDomainResult(\n\t\t\tString resultVariable,\n\t\t\tDomainResultCreationState creationState) {\n\t\tfinal var resolvedExpression = getResolvedExpression();\n\t\tif ( resolvedExpression instanceof SqlTuple ) {\n\t\t\tthrow new SemanticException( \"Composite query parameter cannot be used in select\" );\n\t\t}\n\n\t\tfinal var jdbcMapping = resolvedExpression.getExpressionType().getSingleJdbcMapping();\n\t\tfinal var sqlAstCreationState = creationState.getSqlAstCreationState();\n\t\tfinal var sqlSelection =\n\t\t\t\tsqlAstCreationState.getSqlExpressionResolver()\n\t\t\t\t\t\t.resolveSqlSelection(\n\t\t\t\t\t\t\t\tresolvedExpression,\n\t\t\t\t\t\t\t\tjdbcMapping.getJdbcJavaType(),\n\t\t\t\t\t\t\t\tnull,\n\t\t\t\t\t\t\t\tsqlAstCreationState.getCreationContext().getTypeConfiguration()\n\t\t\t\t\t\t);\n\t\treturn new BasicResult(\n\t\t\t\tsqlSelection.getValuesArrayPosition(),\n\t\t\t\tresultVariable,\n\t\t\t\tjdbcMapping.getMappedJavaType(),\n\t\t\t\tjdbcMapping.getValueConverter(),\n\t\t\t\tnull,","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/internal/SqmParameterInterpretation.java#L69-L105","documentation":"When a query parameter's resolved SQL expression is an SqlTuple (multiple columns - an embeddable, composite user type, or composite/embeddedId key), it cannot be projected as a single select item: JDBC has no single binding for a tuple in the select list. SqmParameterInterpretation.createDomainResult therefore throws SemanticException('Composite query parameter cannot be used in select') at SQL-AST creation time. The query fails at creation, never at execution.","triggerScenarios":"HQL 'select :empId from Employee e' where :empId is bound to an EmbeddedId/composite value; criteria multiselect(cb.parameter(EmbeddedId.class)) or selecting a parameter whose type resolves to an embeddable; 'select e.id from ...' variants where e.id is an embedded id get inlined fine, but explicitly binding a composite parameter into the select list triggers it","commonSituations":"Reports trying to echo a composite filter value back as a result column; mapping a primary key as @EmbeddedId (composite key) and writing generic queries that project bound parameters; migrating from Hibernate 5 where some tuple-in-select cases were tolerated or rendered differently.","solutions":["Select the component attributes explicitly: 'select :empId.part1, :empId.part2 ...' is not supported either - instead select the entity's own components 'select e.id.a, e.id.b' and bind the parameter only in WHERE","Bind the composite parameter only in comparison predicates (where e.id = :empId), never in SELECT or ORDER BY","If you need the value back, return it from Java: query the where clause with the parameter and prepend the bound value client-side","Avoid parameters as select items entirely - use literals or entity attributes in the select list"],"exampleFix":"-- before\nselect :deptPk from Department d where d.pk = :deptPk -- deptPk is @EmbeddedId\n-- after\nselect d.pk.code, d.pk.region from Department d where d.pk = :deptPk","handlingStrategy":"validation","validationCode":"// Reject composite parameters before query creation if they appear as select items\nif (paramValue != null && em.getMetamodel() instanceof MetamodelImpl) {\n    Class<?> pc = paramValue.getClass();\n    boolean embeddable = pc.isAnnotationPresent(jakarta.persistence.Embeddable.class);\n    if (embeddable && hqlSelectItems.contains(\":\" + paramName)) throw new IllegalArgumentException(\"composite param in select\");\n}","typeGuard":"static boolean isComposite(EntityManager em, Object v) {\n    return v != null && em.getMetamodel().getEmbeddables().stream()\n        .anyMatch(et -> et.getJavaType() == v.getClass());\n}","tryCatchPattern":"catch (SemanticException e) { if (e.getMessage().contains(\"Composite query parameter\")) { /* move param to WHERE, select entity components instead */ } else throw e; }","preventionTips":["Never place bound parameters in the select list; parameters belong in predicates","Project entity attributes (e.id.part), not parameter echoes","Add a query lint for ':name' occurring between SELECT and FROM"],"tags":["hibernate","hql","jpa-criteria","composite-key","embedded-id","query-parameter","select-clause"],"backgroundTag":"composite-parameter-in-select","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}