hibernate/hibernate-orm · error · UnsupportedOperationException

Correlated derived root does not have an entity type. Use ge

Error message

Correlated derived root does not have an entity type. Use getReferencedPathSource() instead.

What it means

SqmCorrelatedDerivedRootJoin is the join node created when an inner query correlates a join whose referent is a derived (subquery-based) join. Like the correlated derived root, it wraps query-derived rows, so getModel() - the JPA From method returning an EntityType - throws UnsupportedOperationException. The message directs you to getReferencedPathSource() for the actual row type.

Source

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

			);
		}
		rootJoin.addSqmJoin( correlatedJoin );
		return rootJoin;
	}

	@Override
	public boolean containsOnlyInnerJoins() {
		// The derived join is just referenced, no need to create any table groups
		return true;
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// JPA

	@Nonnull
	@Override
	public SqmEntityDomainType<T> getModel() {
		throw new UnsupportedOperationException( "Correlated derived root does not have an entity type. Use getReferencedPathSource() instead." );
	}

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

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

View on GitHub (pinned to fad1729dce)

Solutions

  1. Call getReferencedPathSource() / getResolvedModel() on the correlated derived join instead of getModel().
  2. Type-check each From element (skip SqmCorrelatedDerivedRootJoin / SqmDerivedJoin) before requesting entity metadata in generic code.
  3. Rewrite the correlated join against the underlying entity when a true EntityType is required for the plan.
  4. Cover join-walking utilities with tests that include derived joins.

Example fix

// before - fails for a join correlated from a derived join
EntityType<?> et = (EntityType<?>) correlatedJoin.getModel();

// after - read the referenced path source of the derived join
SqmPathSource<?> src = ((SqmCorrelatedDerivedRootJoin<?>) correlatedJoin).getReferencedPathSource();
Defensive patterns

Strategy: type-guard

Validate before calling

static boolean hasEntityType(jakarta.persistence.criteria.From<?, ?> from) {
    return !(from instanceof org.hibernate.query.sqm.tree.spi.domain.SqmCorrelatedDerivedRootJoin)
        && !(from instanceof org.hibernate.query.sqm.tree.spi.domain.SqmDerivedJoin)
        && !(from instanceof org.hibernate.query.sqm.tree.spi.domain.SqmCteJoin);
}

Type guard

static boolean isEntityJoin(Object fromNode) {
    return fromNode instanceof org.hibernate.query.sqm.tree.spi.from.SqmEntityJoin
        || fromNode instanceof org.hibernate.query.sqm.tree.spi.domain.SqmAttributeJoin<?, ?> a
            && !(fromNode instanceof org.hibernate.query.sqm.tree.spi.domain.SqmDerivedJoin);
}

Try / catch

try {
    EntityType<?> t = (EntityType<?>) from.getModel();
} catch (UnsupportedOperationException e) {
    SqmPathSource<?> src = ((org.hibernate.query.sqm.tree.spi.domain.SqmCorrelatedDerivedRootJoin<?>) from)
            .getReferencedPathSource();
    // proceed with path-source metadata
}

Prevention

When it happens

Trigger: Criteria code calling subquery.correlate(derivedJoin) and then .getModel() on the returned join; HQL where a subquery references an outer "join (select ...) d" and Java code walks the subquery joins asking for their entity models; framework code that resolves EntityType from every From element to plan fetches or builds joins.

Common situations: Fetch/join planners in repository abstractions that iterate all From elements and call getModel(); queries refactored to join derived tables or CTEs while join-planning code assumed only entity joins; Hibernate version upgrades that introduced correlated derived join support and changed which node types generic code receives.

Related errors


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