hibernate/hibernate-orm · error · IllegalArgumentException

Invalid query type provided to root query 'with' method, exp

Error message

Invalid query type provided to root query 'with' method, expecting a root query to use as CTE instead found a subquery

What it means

Error "Invalid query type provided to root query 'with' method, expecting a root query to use as CTE instead found a subquery" thrown in hibernate/hibernate-orm.

Source

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

	}

	@Override
	public <X> X accept(SemanticQueryWalker<X> walker) {
		return walker.visitSelectStatement( this );
	}

	@Override
	public void addParameter(SqmParameter<?> parameter) {
		if ( parameters == null ) {
			parameters = new LinkedHashSet<>();
		}
		parameters.add( parameter );
	}

	@Override
	protected <X> JpaCteCriteria<X> withInternal(String name, AbstractQuery<X> criteria) {
		if ( criteria instanceof SqmSubQuery<?> ) {
			throw new IllegalArgumentException(
					"Invalid query type provided to root query 'with' method, "
					+ "expecting a root query to use as CTE instead found a subquery"
			);
		}
		return super.withInternal( name, criteria );
	}

	@Override
	protected <X> JpaCteCriteria<X> withInternal(
			String name,
			AbstractQuery<X> baseCriteria,
			boolean unionDistinct,
			Function<JpaCteCriteria<X>,
			AbstractQuery<X>> recursiveCriteriaProducer) {
		if ( baseCriteria instanceof SqmSubQuery<?> ) {
			throw new IllegalArgumentException(
					"Invalid query type provided to root query 'with' method, "
					+ "expecting a root query to use as CTE instead found a subquery"

View on GitHub (pinned to fad1729dce)

Solutions

  1. Pass a root query (not a subquery) to the root query 'with' method when registering a CTE.
  2. Build the CTE as a top-level criteria query and pass that instance.
  3. If the CTE is derived from another query, create a fresh root query with the same structure rather than reusing a subquery.

When it happens

Trigger: A subquery is passed to the 'with' method of a root query, which only accepts a root query as CTE.

Common situations: Mixing a CriteriaSubquery into with(); build the CTE as a root-level criteria query instead.


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