From 942e5cdda41bfa8a10aa4f803fcb8905a8f6357f Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 2 Jan 2024 11:38:49 +0100 Subject: [PATCH] HHH-17606 Fix nested generic join path resolution --- .../hql/internal/QualifiedJoinPathConsumer.java | 17 ++++------------- .../query/sqm/tree/domain/AbstractSqmPath.java | 3 +-- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java index d51e57bddb..9f6ecb11c8 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java +++ b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java @@ -187,7 +187,10 @@ public class QualifiedJoinPathConsumer implements DotIdentifierConsumer { boolean isTerminal, boolean allowReuse, SqmCreationState creationState) { - final SqmPathSource subPathSource = subPathSource( lhs, name, creationState ); + final SqmPathSource subPathSource = lhs.getResolvedModel().getSubPathSource( + name, + creationState.getCreationContext().getJpaMetamodel() + ); if ( allowReuse && !isTerminal ) { for ( SqmJoin sqmJoin : lhs.getSqmJoins() ) { if ( sqmJoin.getAlias() == null && sqmJoin.getReferencedPathSource() == subPathSource ) { @@ -200,18 +203,6 @@ public class QualifiedJoinPathConsumer implements DotIdentifierConsumer { return createJoin( lhs, joinType, alias, fetch, isTerminal, allowReuse, creationState, joinSource ); } - private static SqmPathSource subPathSource(SqmFrom lhs, String name, SqmCreationState creationState) { - final SqmPathSource referencedPathSource = lhs.getReferencedPathSource(); - // We need to use referencedPathSource when it is not generic since the getResolvedModel() method would - // return the association attribute as a path source and for treated paths that might correspond to a - // different entity type (usually the first in alphabetical order) and not the correct treat target - final SqmPathSource pathSource = - referencedPathSource.isGeneric() - ? lhs.getResolvedModel() - : referencedPathSource; - return pathSource.getSubPathSource( name, creationState.getCreationContext().getJpaMetamodel() ); - } - private static SqmFrom createJoin( SqmFrom lhs, SqmJoinType joinType, diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmPath.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmPath.java index 6a7ca0a765..2fae3a9beb 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmPath.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmPath.java @@ -162,8 +162,7 @@ public abstract class AbstractSqmPath extends AbstractSqmExpression implem public SqmPathSource getResolvedModel() { final DomainType lhsType; final SqmPathSource pathSource = getReferencedPathSource(); - if ( pathSource.isGeneric() && ( lhsType = getLhs().getReferencedPathSource() - .getSqmPathType() ) instanceof ManagedDomainType ) { + if ( pathSource.isGeneric() && ( lhsType = getLhs().getResolvedModel().getSqmPathType() ) instanceof ManagedDomainType ) { final PersistentAttribute concreteAttribute = ( (ManagedDomainType) lhsType ).findConcreteGenericAttribute( pathSource.getPathName() );