hibernate/hibernate-orm · error · StrictJpaComplianceViolation

UNMAPPED_POLYMORPHISM

UNMAPPED_POLYMORPHISM

Error message

Encountered unmapped polymorphic reference [${entityName}], but strict JPQL compliance was requested

What it means

Raised in root resolution: the from-clause entity name resolved to an unmapped polymorphic reference (SqmPolymorphicRootDescriptor) while strict JPQL query compliance is enabled, so Hibernate refuses to run the Hibernate-specific polymorphic query (StrictJpaComplianceViolation.Type.UNMAPPED_POLYMORPHISM). The message reports the Hibernate entity name of the polymorphic descriptor.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java:2121

	@Override
	public SqmRoot<?> visitRootEntity(HqlParser.RootEntityContext ctx) {
		final var entityNameContext = ctx.entityName();
		final String name = getEntityName( entityNameContext );

		final var entityDescriptor = creationContext.getJpaMetamodel().getHqlEntityReference( name );

		final String alias = extractAlias( ctx.variable() );

		final var processingState = processingStateStack.getCurrent();
		final var pathRegistry = processingState.getPathRegistry();
		if ( entityDescriptor == null ) {
			return resolveRootEntity( entityNameContext, name, alias, processingState, pathRegistry );
		}
		checkFQNEntityNameJpaComplianceViolationIfNeeded( name, entityDescriptor );

		if ( entityDescriptor instanceof SqmPolymorphicRootDescriptor ) {
			if ( getCreationOptions().useStrictJpaCompliance() ) {
				throw new StrictJpaComplianceViolation(
						"Encountered unmapped polymorphic reference ["
								+ entityDescriptor.getHibernateEntityName()
								+ "], but strict JPQL compliance was requested",
						StrictJpaComplianceViolation.Type.UNMAPPED_POLYMORPHISM
				);
			}

			if ( processingStateStack.depth() > 1 ) {
				throw new SemanticException(
						"Implicitly-polymorphic domain path in subquery '" + entityDescriptor.getName() + "'",
						query
				);
			}
		}

		final var sqmRoot = new SqmRoot<>( entityDescriptor, alias, true, nodeBuilder() );
		pathRegistry.register( sqmRoot );
		return sqmRoot;

View on GitHub (pinned to fad1729dce)

Solutions

  1. Replace the polymorphic name with a concrete entity name
  2. Set 'hibernate.jpa.compliance.query=false' to re-enable the Hibernate extension
  3. Scope compliance to the queries that need it instead of the whole SessionFactory

Example fix

// before
List<Object> rows = em.createQuery("from java.lang.Object o", Object.class).getResultList();  // with compliance on

// after
List<Employee> rows = em.createQuery("from Employee e", Employee.class).getResultList();
Defensive patterns

Strategy: fallback

Validate before calling

static boolean polymorphicRoot(EntityManagerFactory emf, String name) {
    // a name that resolves to zero or many entities behaves polymorphically in HQL
    long n = emf.getMetamodel().getEntities().stream().filter(e -> e.getName().equals(name)).count();
    return n == 0 && name.contains("."); // namespace/object style names
}

if (polymorphicRoot(emf, name) && strictQueryCompliance(emf)) {
    throw new IllegalArgumentException("Polymorphic root not allowed under strict JPA compliance: " + name);
}

Type guard

static boolean isStrictJpaViolation(Throwable t) {
    return t instanceof org.hibernate.query.sqm.StrictJpaComplianceViolation;
}

Try / catch

try {
    return em.createQuery("from " + name + " o", Object.class).getResultList();
} catch (org.hibernate.query.sqm.StrictJpaComplianceViolation e) {
    return em.createQuery("from " + concreteEntityName + " o", Object.class).getResultList(); // concrete fallback
}

Prevention

When it happens

Trigger: 'from java.lang.Object o' or a package-namespace polymorphic root (e.g. 'from com.acme.domain') with 'hibernate.jpa.compliance.query=true' (or global 'hibernate.jpa.compliance=true').

Common situations: Enabling JPA compliance globally for certification/consistency while existing code uses Hibernate's polymorphic HQL feature; Spring Boot configs inheriting compliance settings from a shared baseline.

Related errors


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