hibernate/hibernate-orm · error · TreatException

Embeddable paths cannot be TREAT-ed to an entity type

Error message

Embeddable paths cannot be TREAT-ed to an entity type

What it means

Thrown by Hibernate's SQM tree when TREAT is applied to an embeddable-valued path with an entity type as the target. JPA TREAT is a downcast within an entity inheritance hierarchy; embeddables have no identity and no inheritance, so SqmEmbeddedValuedSimplePath.treatAs(EntityDomainType) unconditionally throws TreatException (a HibernateException). Note that the Class-based overload only accepts another embeddable type resolved from the domain model.

Source

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

		return sqmPath;
	}

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


	@Nonnull
	@Override
	public <S extends T> SqmTreatedPath<T, S> treatAs(@Nonnull Class<S> treatJavaType) {
		return getTreatedPath( nodeBuilder().getDomainModel().embeddable( treatJavaType ) );
	}

	@Nonnull
	@Override
	public <S extends T> SqmTreatedPath<T, S> treatAs(@Nonnull EntityDomainType<S> treatTarget) {
		throw new TreatException( "Embeddable paths cannot be TREAT-ed to an entity type" );
	}

	@Override
	public JavaType<T> getExpressibleJavaType() {
		return super.getExpressible().getExpressibleJavaType();
	}

	@Override
	public @Nonnull Class<T> getJavaType() {
		return getJavaTypeDescriptor().getJavaTypeClass();
	}

	@Override
	public JavaType<?> getRelationalJavaType() {
		return super.getExpressible().getRelationalJavaType();
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. If the attribute is @Embedded and you need polymorphism, remodel it as a @ManyToOne/@OneToOne entity association and apply TREAT to that association path
  2. If you intended Hibernate 7 embeddable treat, make sure the target is an embeddable type registered in the domain model (treatAs(Class) resolves it via embeddable(treatJavaType)), not an entity
  3. If no polymorphism is needed, drop the TREAT and navigate the embeddable's sub-attributes directly (p.address.zip)
  4. Check the mapping annotations: @Embedded vs @ManyToOne on the referenced attribute decides whether TREAT can ever apply

Example fix

// before: address is @Embeddable, USAddress is an @Entity
// HQL: select a.zip from Person p join treat(p.address as USAddress) a
//  -> TreatException: Embeddable paths cannot be TREAT-ed to an entity type

// after: model it as an entity association with inheritance
// @Entity class Address { ... }
// @Entity class USAddress extends Address { ... }
// @ManyToOne Address address;
// HQL: select a.zip from Person p join treat(p.address as USAddress) a
Defensive patterns

Strategy: validation

Validate before calling

// before calling treatAs on a path, check its referenced type
if (path.getReferencedPathSource().getPathType() instanceof EmbeddableDomainType<?>) {
    // treatAs(EntityDomainType) would throw TreatException:
    // either navigate sub-attributes or remodel as an entity association
    return path.get(subAttributeName);
}
return path.treatAs(targetEntity);

Type guard

static boolean isEntityValuedPath(SqmPath<?> path) {
    return path.getReferencedPathSource().getPathType() instanceof IdentifiableDomainType<?>;
}

Try / catch

try {
    treated = path.treatAs(target);
} catch (TreatException e) {
    // path is embeddable/basic: report a query-model error, do not retry with the same target
    throw new IllegalArgumentException("TREAT unsupported on " + path.getNavigablePath(), e);
}

Prevention

When it happens

Trigger: Calling path.treatAs(SomeEntity.class) or path.treatAs(entityDomainType) on a path whose referenced source is an @Embedded/@EmbeddedId attribute, e.g. root.get("address").treatAs(AddressEntity.class) in criteria, or HQL like 'join treat(p.address as USAddress) a' where address is @Embedded and USAddress is an @Entity.

Common situations: Modeling polymorphic embedded data with entity classes and then trying to TREAT it; confusing an @Embedded attribute with a @ManyToOne association when porting queries; users discovering Hibernate 7's embeddable-treat support and passing an entity class as target; migrating native SQL CASE-on-type idioms into HQL TREAT.

Related errors


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