hibernate/hibernate-orm · error · IllegalStateException

Entity discriminator cannot be de-referenced

Error message

Entity discriminator cannot be de-referenced

What it means

Same rule as the entity discriminator, but for @Any mappings: the discriminator of an ANY-valued path (AnyDiscriminatorSqmPathSource) is a scalar that names the target entity and cannot be navigated. Any attempt to resolve a sub-path source under it throws IllegalStateException from findSubPathSource.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AnyDiscriminatorSqmPathSource.java:51

			SimpleDomainType<D> domainType,
			BindableType jpaBindableType) {
		super( localPathName, pathModel, domainType, jpaBindableType );
		this.domainType = (BasicType<D>) domainType; // TODO: don't like this cast!
	}

	@Override
	public SqmPath<D> createSqmPath(SqmPath<?> lhs, @Nullable SqmPathSource<?> intermediatePathSource) {
		final var path = lhs.getNavigablePath();
		final var navigablePath =
				intermediatePathSource == null
						? path
						: path.append( intermediatePathSource.getPathName() );
		return new AnyDiscriminatorSqmPath<>( navigablePath, pathModel, lhs, lhs.nodeBuilder() );
	}

	@Override
	public SqmPathSource<?> findSubPathSource(String name) {
		throw new IllegalStateException( "Entity discriminator cannot be de-referenced" );
	}

	@Override
	@Nonnull
	public PersistenceType getPersistenceType() {
		return BASIC;
	}

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

	@Override
	public @Nullable SqmDomainType<D> getSqmType() {
		return getPathType();
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Use the discriminator only for equality/TYPE comparisons; to access target fields, join or load the referenced entity via the ANY's key plus its discriminator-resolved entity type
  2. In path-walking code, skip sources named "type"/"class" or instanceof AnyDiscriminatorSqmPathSource before findSubPathSource
  3. Replace @Any with a real @ManyToOne to a shared supertype if navigable references are required

Example fix

// before (HQL)
select o.customer.type.name from Order o   // ANY discriminator dereference -> throws

// after
select o.customer.name from Order o          // treat as association, or
select o.customer.type from Order o           // scalar compare only
Defensive patterns

Strategy: validation

Validate before calling

// Skip synthetic discriminator sources when expanding @Any paths
String name = pathSource.getPathName();
if ("type".equals(name) || "class".equals(name)
        || pathSource instanceof org.hibernate.metamodel.model.domain.internal.AnyDiscriminatorSqmPathSource<?>) {
    return; // scalar discriminator: compare only, never navigate
}

Type guard

static boolean isAnyDiscriminatorSource(org.hibernate.query.sqm.tree.SqmPathSource<?> src) {
    return src instanceof org.hibernate.metamodel.model.domain.internal.AnyDiscriminatorSqmPathSource<?>;
}

Try / catch

try {
    SqmPathSource<?> sub = source.findSubPathSource(name);
} catch (IllegalStateException e) {
    if ("Entity discriminator cannot be de-referenced".equals(e.getMessage())) {
        // treat the @Any discriminator as a leaf; do not expand
    } else throw e;
}

Prevention

When it happens

Trigger: HQL/Criteria dereferencing the discriminator of an @Any property, e.g. order.customer.anyKeyDiscriminator.name, or generic path-expansion code calling findSubPathSource on the synthetic discriminator source of an ANY attribute. Also joining through the discriminator as if it were an association.

Common situations: Polymorphic @Any/@AnyDiscriminator+@AnyJoinColumn mappings (order.customer can point to any of several entity types) combined with generic query builders or GraphQL-style resolvers that expand every path. Confusing the ANY discriminator (picks the entity type) with an association you can join.

Related errors


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