hibernate/hibernate-orm · error · SyntaxException

Position based 'order by' is not allowed in 'over' or…

Error message

Position based 'order by' is not allowed in 'over' or 'within group' clauses

What it means

Thrown by SemanticQueryBuilder.resolveOrderByOrGroupByExpression when a positional 'order by' item (e.g. 'order by 1') is used inside an 'over' or 'within group' clause of a window/ordered-set function. Positional ordering references are not resolvable in those clauses, so the semantic layer rejects them with a SyntaxException.

Solutions

  1. Use expression-based ordering inside 'over'/'within group' clauses.
  2. Remove the positional reference.
Defensive patterns

Strategy: validation

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/5e0d84519f184a38. Report an issue: GitHub.

Appendix: source

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

		return expressions;
	}

	private SqmExpression<?> resolveOrderByOrGroupByExpression(
			ParseTree child,
			boolean definedCollate,
			boolean allowPositionalOrAliases) {
		final var nodeBuilder = nodeBuilder();
		final var processingState = processingStateStack.getCurrent();
		final var processingQuery = processingState.getProcessingQuery();
		final var queryPart = sqmQueryPart( processingQuery );
		if ( child instanceof TerminalNode ) {
			if ( definedCollate ) {
				// This is syntactically disallowed
				throw new SyntaxException( "'collate' is not allowed for position based 'order by' or 'group by' items" );
			}
			else if ( !allowPositionalOrAliases ) {
				// This is syntactically disallowed
				throw new SyntaxException( "Position based 'order by' is not allowed in 'over' or 'within group' clauses" );
			}

			final int position = Integer.parseInt( child.getText() );

			// make sure this selection exists
			final var nodeByPosition = nodeByPosition( queryPart, position, processingState );
			if ( nodeByPosition == null ) {
				throw new SemanticException( "Numeric literal '" + position
						+ "' used in 'group by' does not match a registered select item",
						query );
			}

			return new SqmAliasedNodeRef(
					position,
					nodeBuilder.resolveExpressible( integerDomainType ),
					nodeBuilder
			);
		}

View on GitHub (pinned to fad1729dce)