{"record":{"id":"c638aa3a8b506101","repo":"hibernate/hibernate-orm","slug":"query-has-having-but-no-group-by","errorCode":null,"errorMessage":"Query has 'having' but no 'group by'","messagePattern":"Query has 'having' but no 'group by'","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":1191,"sourceCode":"\t\t\tsqmQueryPart.setFetchExpression( (SqmExpression<? extends Number>) visitLimitClause(limitClauseContext) );\n\t\t}\n\t\telse {\n\t\t\tthrow new SemanticException(\"The 'limit' and 'fetch' clauses may not be used together\", query );\n\t\t}\n\t}\n\n\t@Override\n\tpublic SqmQuerySpec<?> visitQuery(HqlParser.QueryContext ctx) {\n\t\tfinal var sqmQuerySpec = currentQuerySpec();\n\n\t\tfinal var fromClauseContext = ctx.fromClause();\n\t\tfinal var whereClauseContext = ctx.whereClause();\n\t\tfinal var groupByClauseContext = ctx.groupByClause();\n\t\tfinal var havingClauseContext = ctx.havingClause();\n\t\tfinal var selectClauseContext = ctx.selectClause();\n\n\t\tif ( havingClauseContext != null && groupByClauseContext == null ) {\n\t\t\tthrow new SemanticException( \"Query has 'having' but no 'group by'\", query );\n\t\t}\n\n\t\t// visit from clause first!!!\n\t\tfinal var fromClause =\n\t\t\t\tfromClauseContext == null\n\t\t\t\t\t\t? buildInferredFromClause( selectClauseContext )\n\t\t\t\t\t\t: visitFromClause( fromClauseContext );\n\t\tsqmQuerySpec.setFromClause( fromClause );\n\n\t\tfinal var selectClause =\n\t\t\t\tselectClauseContext == null\n\t\t\t\t\t\t? buildInferredSelectClause( fromClause )\n\t\t\t\t\t\t: visitSelectClause( selectClauseContext );\n\t\tsqmQuerySpec.setSelectClause( selectClause );\n\n\t\tfinal var whereClause = new SqmWhereClause( nodeBuilder() );\n\t\tif ( whereClauseContext != null ) {\n\t\t\twhereClause.setPredicate( (SqmPredicate) whereClauseContext.accept( this ) );","sourceCodeStart":1173,"sourceCodeEnd":1209,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L1173-L1209","documentation":"Thrown by SemanticQueryBuilder.visitQuery when the query spec declares a 'having' clause but no 'group by' clause. 'Having' filters aggregated groups and is only meaningful together with grouping (a bare 'having' without grouping is not supported by Hibernate's HQL semantics), so the ungrouped query part is rejected with a SemanticException.","triggerScenarios":"The HQL/JPQL query violates a structural language rule.","commonSituations":"Writing queries with missing select/from, mismatched group by positions, or invalid ordering constructs.","solutions":["Add a 'group by' clause that matches the having conditions.","Move non-aggregate conditions to the where clause."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}