hibernate/hibernate-orm · error · SemanticException

The 'limit' and 'fetch' clauses may not be used together

Error message

The 'limit' and 'fetch' clauses may not be used together

What it means

Error "The 'limit' and 'fetch' clauses may not be used together" thrown in hibernate/hibernate-orm.

Source

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

			sqmQueryPart.setOrderByClause( visitOrderByClause( ctx ) );
		}
	}

	@SuppressWarnings("unchecked")
	private void setOffsetFetchLimit(SqmQueryPart<?> sqmQueryPart, HqlParser.LimitClauseContext limitClauseContext, HqlParser.OffsetClauseContext offsetClauseContext, HqlParser.FetchClauseContext fetchClauseContext) {
		// these casts are all fine because the parser only accepts literals and parameters
		sqmQueryPart.setOffsetExpression( (SqmExpression<? extends Number>) visitOffsetClause(offsetClauseContext) );
		if ( limitClauseContext == null ) {
			sqmQueryPart.setFetchExpression(
					(SqmExpression<? extends Number>) visitFetchClause(fetchClauseContext),
					visitFetchClauseType(fetchClauseContext)
			);
		}
		else if ( fetchClauseContext == null ) {
			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!!!

View on GitHub (pinned to fad1729dce)

Solutions

  1. Use either 'limit' or 'fetch first', not both, in the query.
  2. Remove one of the limiting clauses.

When it happens

Trigger: A runtime precondition of the Hibernate API is violated.

Common situations: Occurs when application code or configuration does not satisfy the documented contract of the API being called.


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