hibernate/hibernate-orm · error · IllegalStateException

SortSpecification is missing selection indexes

Error message

SortSpecification is missing selection indexes

What it means

Error "SortSpecification is missing selection indexes" thrown in hibernate/hibernate-orm.

Source

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

			SortSpecification sortSpecification,
			Nulls nullPrecedence,
			int selectItemCount,
			List<Expression> extraSelections,
			IdentityHashMap<SortSpecification, int[]> nullPrecedenceSelectionIndexes) {
		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,

View on GitHub (pinned to fad1729dce)

Solutions

  1. This is an internal Hibernate assertion indicating the SortSpecification was built without selection indexes. If you see this error, report it as a Hibernate bug with the full stack trace and the query that triggered it.
  2. As a workaround, simplify the ORDER BY clause or the query structure (e.g. remove the emulation of full joins) so the query takes a different code path.

When it happens

Trigger: Hibernate builds a full-join emulation and the SortSpecification carries no selection indexes.

Common situations: ORDER BY on a full join on a dialect without native full-join support (e.g. older MySQL/MariaDB), where the sort keys could not be matched to select-list columns.


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