From ada4701a9568052f45c6bfaf4c520b483966bdd4 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 31 Jul 2023 12:08:26 +0200 Subject: [PATCH] HHH-16988 Fix and simplify the getNavigablePathCopy method Handle implicitly treated navigable paths copy correctly. Also, small change to findPluralAttribute() to correctly handle multiple inheritance types. --- .../model/domain/AbstractManagedType.java | 2 +- .../sqm/tree/domain/AbstractSqmPath.java | 47 +++++++++++-------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractManagedType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractManagedType.java index 85c5a4c0d9..d3ec9eb4c1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractManagedType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractManagedType.java @@ -441,7 +441,7 @@ public abstract class AbstractManagedType return attribute; } else if ( getSuperType() != null ) { - return getSuperType().findDeclaredPluralAttribute( name ); + return getSuperType().findPluralAttribute( name ); } else { return null; 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 8baf29a254..a17692ec2b 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 @@ -28,6 +28,7 @@ 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 org.hibernate.spi.TreatedNavigablePath; import jakarta.persistence.metamodel.MapAttribute; import jakarta.persistence.metamodel.PluralAttribute; @@ -227,37 +228,45 @@ 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) { - return getNavigablePathCopy( castNonNull( navigablePath.getRealParent() ), parent.getNavigablePath(), false, null ); + final NavigablePath realParentPath = getRealParentPath( + castNonNull( navigablePath.getRealParent() ), + parent.getNavigablePath() + ); + if ( realParentPath != null ) { + return realParentPath.append( navigablePath.getLocalName(), navigablePath.getAlias() ); + } + return navigablePath; } - private NavigablePath getNavigablePathCopy( - NavigablePath realParent, - NavigablePath parent, - boolean isId, - String identifierAttributeName) { + private NavigablePath getRealParentPath(NavigablePath realParent, NavigablePath parent) { if ( parent == realParent ) { - return navigablePath; + return null; } - else if ( !isId && realParent instanceof EntityIdentifierNavigablePath ) { - return getNavigablePathCopy( - castNonNull( realParent.getRealParent() ), - parent, - true, - ( (EntityIdentifierNavigablePath) realParent ).getIdentifierAttributeName() - ); + else if ( realParent instanceof EntityIdentifierNavigablePath ) { + parent = getRealParentPath( castNonNull( realParent.getRealParent() ), parent ); + if ( parent != null ) { + parent = new EntityIdentifierNavigablePath( + parent, + ( (EntityIdentifierNavigablePath) realParent ).getIdentifierAttributeName() + ); + } + } + else if ( realParent.getAlias() == null && realParent instanceof TreatedNavigablePath ) { + // This might be an implicitly treated parent path, check with the non-treated parent + parent = getRealParentPath( castNonNull( realParent.getRealParent() ), parent ); + if ( parent != null ) { + parent = parent.treatAs( realParent.getLocalName().substring( 1 ) ); + } } else if ( CollectionPart.Nature.fromNameExact( realParent.getLocalName() ) != null ) { if ( parent == realParent.getRealParent() ) { - return navigablePath; + return null; } else { parent = parent.append( realParent.getLocalName() ); } } - if ( isId ) { - parent = new EntityIdentifierNavigablePath( parent, identifierAttributeName ); - } - return parent.append( navigablePath.getLocalName(), navigablePath.getAlias() ); + return parent; } @Override