From d23d448e72988e01b5dde46f89099f054f11ece3 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 11 Jul 2023 15:25:34 +0200 Subject: [PATCH] HHH-16905 Account for identifiers in getNavigablePathCopy --- .../sqm/tree/domain/AbstractSqmPath.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) 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 36c8f5fef0..8baf29a254 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 @@ -13,7 +13,6 @@ import java.util.List; import java.util.Map; import java.util.function.Consumer; -import org.hibernate.internal.util.NullnessUtil; import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping; import org.hibernate.metamodel.model.domain.DomainType; @@ -27,12 +26,15 @@ import org.hibernate.query.sqm.tree.SqmCopyContext; import org.hibernate.query.sqm.tree.expression.AbstractSqmExpression; import org.hibernate.query.sqm.tree.expression.SqmExpression; import org.hibernate.query.sqm.tree.expression.SqmLiteral; +import org.hibernate.spi.EntityIdentifierNavigablePath; import org.hibernate.spi.NavigablePath; import jakarta.persistence.metamodel.MapAttribute; import jakarta.persistence.metamodel.PluralAttribute; import jakarta.persistence.metamodel.SingularAttribute; +import static org.hibernate.internal.util.NullnessUtil.castNonNull; + /** * @author Steve Ebersole */ @@ -225,21 +227,37 @@ public abstract class AbstractSqmPath extends AbstractSqmExpression implem * and if not creates a copy of the navigable path with the correct parent. */ protected NavigablePath getNavigablePathCopy(SqmPath parent) { - final NavigablePath parentNavigablePath = NullnessUtil.castNonNull( navigablePath.getRealParent() ); - if ( parent.getNavigablePath() == parentNavigablePath ) { + return getNavigablePathCopy( castNonNull( navigablePath.getRealParent() ), parent.getNavigablePath(), false, null ); + } + + private NavigablePath getNavigablePathCopy( + NavigablePath realParent, + NavigablePath parent, + boolean isId, + String identifierAttributeName) { + if ( parent == realParent ) { return navigablePath; } - else if ( CollectionPart.Nature.fromNameExact( parentNavigablePath.getLocalName() ) != null ) { - if ( parent.getNavigablePath() == parentNavigablePath.getRealParent() ) { + else if ( !isId && realParent instanceof EntityIdentifierNavigablePath ) { + return getNavigablePathCopy( + castNonNull( realParent.getRealParent() ), + parent, + true, + ( (EntityIdentifierNavigablePath) realParent ).getIdentifierAttributeName() + ); + } + else if ( CollectionPart.Nature.fromNameExact( realParent.getLocalName() ) != null ) { + if ( parent == realParent.getRealParent() ) { return navigablePath; } else { - return parent.getNavigablePath() - .append( parentNavigablePath.getLocalName() ) - .append( navigablePath.getLocalName(), navigablePath.getAlias() ); + parent = parent.append( realParent.getLocalName() ); } } - return parent.getNavigablePath().append( navigablePath.getLocalName(), navigablePath.getAlias() ); + if ( isId ) { + parent = new EntityIdentifierNavigablePath( parent, identifierAttributeName ); + } + return parent.append( navigablePath.getLocalName(), navigablePath.getAlias() ); } @Override