hibernate/hibernate-orm · error · IllegalStateException

Query group can't be treated as query spec. Use JpaSelectCri

Error message

Query group can't be treated as query spec. Use JpaSelectCriteria#getQueryPart to access query group details

What it means

Error "Query group can't be treated as query spec. Use JpaSelectCriteria#getQueryPart to access query group details" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSelectStatement.java:198

	@Nonnull
	@Override
	public List<Order> getOrderList() {
		return unmodifiableList( getQueryPart().getSortSpecifications() );
	}

	@Override
	public SqmQuerySource getQuerySource() {
		return querySource;
	}

	@Nonnull
	@Override
	public SqmQuerySpec<T> getQuerySpec() {
		if ( querySource == CRITERIA ) {
			if ( getQueryPart() instanceof SqmQuerySpec<T> querySpec ) {
				return querySpec;
			}
			throw new IllegalStateException(
					"Query group can't be treated as query spec. Use JpaSelectCriteria#getQueryPart to access query group details"
			);
		}
		else {
			return super.getQuerySpec();
		}
	}

	public boolean producesUniqueResults() {
		// For query groups we have to assume that duplicates are possible
		return !( getQueryPart() instanceof SqmQuerySpec<?> querySpec )
			|| querySpec.producesUniqueResults();
	}

	public boolean containsCollectionFetches() {
		return containsCollectionFetches( getQueryPart() );
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Use JpaSelectCriteria#getQueryPart to access the query group instead of calling query-spec methods on it.
  2. Check whether the statement represents a query group (UNION etc.) before calling single-query methods.
  3. If you only need the first query part, navigate through getQueryPart() and cast to the spec you need.

When it happens

Trigger: Code accesses a select statement's query spec although the statement is a query group (set operations).

Common situations: Calling getQuerySpec() on a UNION query; use JpaSelectCriteria#getQueryPart to inspect the group.


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