hibernate/hibernate-orm · error · IllegalStateException

SortSpecification selection indexes size mismatch

Error message

SortSpecification selection indexes size mismatch

What it means

Error "SortSpecification selection indexes size mismatch" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/sql/ast/spi/FullJoinEmulation.java:675

		final var expressions = getSortExpressions( sortSpecification );
		final int[] indexes = new int[expressions.size()];
		for ( int i = 0; i < expressions.size(); i++ ) {
			extraSelections.add( createSortNullPrecedenceEmulationExpression( expressions.get( i ), nullPrecedence ) );
			indexes[i] = selectItemCount + extraSelections.size() - 1;
		}
		nullPrecedenceSelectionIndexes.put( sortSpecification, indexes );
	}

	private List<? extends Expression> getSortExpressions(SortSpecification sortSpecification) {
		final var sortExpression = sortSpecification.getSortExpression();
		final int[] sortSelectionIndexes = sortSpecification.getSortSelectionIndexes();
		if ( sortSelectionIndexes == null ) {
			throw new IllegalStateException( "SortSpecification is missing selection indexes" );
		}
		final var tuple = getSqlTuple( sortExpression );
		final var expressions = tuple == null ? List.of( sortExpression ) : tuple.getExpressions();
		if ( sortSelectionIndexes.length != expressions.size() ) {
			throw new IllegalStateException( "SortSpecification selection indexes size mismatch" );
		}
		return expressions;
	}

	private Nulls resolveRenderedNullPrecedence(SortSpecification sortSpecification) {
		final var sortOrder = sortSpecification.getSortOrder();
		Nulls nullPrecedence = sortSpecification.getNullPrecedence();
		if ( nullPrecedence == null || nullPrecedence == Nulls.NONE ) {
			nullPrecedence =
					translator.getSessionFactory().getSessionFactoryOptions()
							.getDefaultNullPrecedence();
		}
		if ( isDefaultOrdering( nullPrecedence, sortOrder,
				translator.getDialect().getNullOrdering() ) ) {
			return null;
		}
		return nullPrecedence;
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. This is an internal Hibernate consistency check during full-join emulation: the number of selection indexes does not match the expected size. Report it as a Hibernate bug with the stack trace and the originating query.
  2. As a workaround, restructure the query to avoid full-join emulation (e.g. rewrite the join type or split the query).

When it happens

Trigger: The number of selection indexes in the SortSpecification does not match the expected sort specification size during full-join emulation.

Common situations: Full outer join with ORDER BY on a dialect that emulates full joins via UNION, when sort items cannot be aligned with the emulated select list.


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