hibernate/hibernate-orm · error · UnsupportedOperationException

Function roots can not be treated

Error message

Function roots can not be treated

What it means

TREAT is defined for entity inheritance hierarchies; a SqmFunctionRoot (a root derived from a function call) does not participate in one, so treatAs throws UnsupportedOperationException. Filter predicates on the function result's columns are the supported alternative to downcasting.

Solutions

  1. Replace TREAT with WHERE conditions on the function result's type/kind attribute (e.g. where g.kind = 'SUB')
  2. Move the polymorphic part to a real entity association elsewhere in the query and TREAT that instead
  3. Restructure so the function returns a single concrete type and do subtype filtering in SQL or Java

Example fix

// before
from json_rows(p.doc) g join treat(g as SpecialRow) s -- treat on function root

// after
from json_rows(p.doc) g where g.kind = 'SPECIAL'
-- filter on the type/kind column; or join a real entity association and TREAT that
Defensive patterns

Strategy: type-guard

Type guard

static boolean treatableRoot(SqmRoot<?> root) {
    return !(root instanceof SqmFunctionRoot<?>);
}

Try / catch

try {
    SqmTreatedFrom<?, ?, ?> t = root.treatAs(target, alias, false);
} catch (UnsupportedOperationException e) {
    // function roots are never treatable: replace with a WHERE type/kind predicate
    throw new IllegalStateException("Use a filter predicate instead of TREAT on function roots", e);
}

Prevention

When it happens

Trigger: HQL 'treat(f(x) as Sub)' in the FROM clause, or criteria calling functionRoot.treatAs(SubType.class, alias, false) on a function-derived root.

Common situations: Applying boilerplate TREAT patterns to new function-root queries; set-returning functions that conceptually return different row kinds; porting SQL's joins over type-discriminated subqueries into HQL.

Related errors


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmFunctionRoot.java:121

	public String getEntityName() {
		throw new UnsupportedOperationException( "Function root does not have an entity type. Use getReferencedPathSource() instead." );
	}

	@Override
	public SqmPathSource<E> getResolvedModel() {
		return getReferencedPathSource();
	}

	@Override
	@Nonnull
	public SqmCorrelatedRoot<E> createCorrelation() {
		throw new UnsupportedOperationException();
	}

	@Override
	@Nonnull
	public <S extends E> SqmTreatedFrom<E, E, S>  treatAs(@Nonnull EntityDomainType<S> treatTarget, @Nullable String alias, boolean fetch) {
		throw new UnsupportedOperationException( "Function roots can not be treated" );
	}

	@Override
	public boolean deepEquals(SqmFrom<?, ?> object) {
		return super.deepEquals( object )
			&& function.equals( ((SqmFunctionRoot<?>) object).function );
	}

	@Override
	public boolean isDeepCompatible(SqmFrom<?, ?> object) {
		return super.isDeepCompatible( object )
			&& function.isCompatible( ((SqmFunctionRoot<?>) object).function );
	}
}

View on GitHub (pinned to fad1729dce)