hibernate/hibernate-orm · error · SyntaxException

'collate' is not allowed for position based 'order by' or…

Error message

'collate' is not allowed for position based 'order by' or 'group by' items

What it means

Thrown by SemanticQueryBuilder.resolveOrderByOrGroupByExpression when a positional 'order by'/'group by' item (an integer like 'order by 1') is combined with a 'collate' specification. Collation applies to expression values, not to positional references, so the parser/semantic layer rejects the combination with a SyntaxException.

Solutions

  1. Remove 'collate' from position-based order/group-by items.
  2. Use alias- or path-based items when collation is needed.
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/4d3189235372237e. Report an issue: GitHub.

Appendix: source

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

		final List<SqmExpression<?>> expressions = new ArrayList<>( size );
		for ( int i = 0; i < size; i++ ) {
			expressions.add( (SqmExpression<?>) groupByExpressionContexts.get( i ).accept( this ) );
		}
		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,

View on GitHub (pinned to fad1729dce)