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
- 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
- In path-walking code, skip sources named "type"/"class" or instanceof AnyDiscriminatorSqmPathSource before findSubPathSource
- 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
- Model @Any discriminators as comparison-only scalars in query builders
- Prefer @ManyToOne to a supertype when navigation is needed
- Cover @Any attributes in path-expansion unit tests
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
- Entity discriminator cannot be de-referenced
- Cannot apply TREAT operator to discriminator path
- Unable to interpret <meta-value value="%s" class="%s"/> defi
- Could not resolve attribute '%s' of '%s' due to the attribut
- The function {name} is not an aggregate function
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/a477f166aec00223.
Report an issue: GitHub.