hibernate/hibernate-orm · error · SemanticException

Query has 'having' but no 'group by'

Error message

Query has 'having' but no 'group by'

What it means

Error "Query has 'having' but no 'group by'" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java:1191

			sqmQueryPart.setFetchExpression( (SqmExpression<? extends Number>) visitLimitClause(limitClauseContext) );
		}
		else {
			throw new SemanticException("The 'limit' and 'fetch' clauses may not be used together", query );
		}
	}

	@Override
	public SqmQuerySpec<?> visitQuery(HqlParser.QueryContext ctx) {
		final var sqmQuerySpec = currentQuerySpec();

		final var fromClauseContext = ctx.fromClause();
		final var whereClauseContext = ctx.whereClause();
		final var groupByClauseContext = ctx.groupByClause();
		final var havingClauseContext = ctx.havingClause();
		final var selectClauseContext = ctx.selectClause();

		if ( havingClauseContext != null && groupByClauseContext == null ) {
			throw new SemanticException( "Query has 'having' but no 'group by'", query );
		}

		// visit from clause first!!!
		final var fromClause =
				fromClauseContext == null
						? buildInferredFromClause( selectClauseContext )
						: visitFromClause( fromClauseContext );
		sqmQuerySpec.setFromClause( fromClause );

		final var selectClause =
				selectClauseContext == null
						? buildInferredSelectClause( fromClause )
						: visitSelectClause( selectClauseContext );
		sqmQuerySpec.setSelectClause( selectClause );

		final var whereClause = new SqmWhereClause( nodeBuilder() );
		if ( whereClauseContext != null ) {
			whereClause.setPredicate( (SqmPredicate) whereClauseContext.accept( this ) );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Add a 'group by' clause that matches the having conditions.
  2. Move non-aggregate conditions to the where clause.

When it happens

Trigger: The HQL/JPQL query violates a structural language rule.

Common situations: Writing queries with missing select/from, mismatched group by positions, or invalid ordering constructs.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/c638aa3a8b506101. Report an issue: GitHub.