hibernate/hibernate-orm · error · IllegalStateException

Cannot fetch the same association twice with a different…

Error message

Cannot fetch the same association twice with a different alias

What it means

Thrown in QualifiedJoinPathConsumer.createJoin while reusing an existing fetch join: a compatible fetch join of the same association was already created, but the new join attempt declares an additional alias. Hibernate cannot assign a second alias to the same fetched association, so an IllegalStateException signals the conflicting alias declaration.

Solutions

  1. Fetch the association once and reuse the alias.
  2. Remove the duplicate fetch join or give it the same alias.
Defensive patterns

Strategy: validation

When it happens

Trigger: A runtime precondition of the Hibernate API is violated.

Common situations: Occurs when application code or configuration does not satisfy the documented contract of the API being called.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java:214

			SqmCreationState creationState) {
		final var subPathSource = lhs.getResolvedModel().getSubPathSource( name, true );
		if ( allowReuse ) {
			if ( !isTerminal ) {
				for ( var sqmJoin : lhs.getSqmJoins() ) {
					// In order for an HQL join to be reusable, it must have the same path source,
					if ( sqmJoin.getModel() == subPathSource
						// and must not have a join condition.
						&& sqmJoin.getJoinPredicate() == null ) {
						// We explicitly allow reusing implicit joins of any type
						return sqmJoin;
					}
				}
			}
			else if ( fetch ) {
				final var compatibleFetchJoin = findCompatibleFetchJoin( lhs, subPathSource, joinType );
				if ( compatibleFetchJoin != null ) {
					if ( alias != null ) {
						throw new IllegalStateException( "Cannot fetch the same association twice with a different alias" );
					}
					return compatibleFetchJoin;
				}
			}
		}
		if ( !(subPathSource instanceof SqmJoinable) ) {
			throw new SemanticException( "Joining on basic value elements is not supported",
					((SemanticQueryBuilder<?>) creationState).getQuery() );
		}
		@SuppressWarnings("unchecked")
		final var joinSource = (SqmJoinable<U, ?>) subPathSource;
		return createJoin( lhs, joinType, alias, fetch, isTerminal, allowReuse, creationState, joinSource );
	}

	private static <U,V> SqmFrom<?, ?> createJoin(
			SqmFrom<?,U> lhs,
			SqmJoinType joinType,
			String alias,

View on GitHub (pinned to fad1729dce)