HHH-16905 Account for identifiers in getNavigablePathCopy

This commit is contained in:
Marco Belladelli 2023-07-11 15:25:34 +02:00
parent 7c181454bc
commit d23d448e72
1 changed files with 27 additions and 9 deletions

View File

@ -13,7 +13,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.hibernate.internal.util.NullnessUtil;
import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping; import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
import org.hibernate.metamodel.model.domain.DomainType; 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.AbstractSqmExpression;
import org.hibernate.query.sqm.tree.expression.SqmExpression; import org.hibernate.query.sqm.tree.expression.SqmExpression;
import org.hibernate.query.sqm.tree.expression.SqmLiteral; import org.hibernate.query.sqm.tree.expression.SqmLiteral;
import org.hibernate.spi.EntityIdentifierNavigablePath;
import org.hibernate.spi.NavigablePath; import org.hibernate.spi.NavigablePath;
import jakarta.persistence.metamodel.MapAttribute; import jakarta.persistence.metamodel.MapAttribute;
import jakarta.persistence.metamodel.PluralAttribute; import jakarta.persistence.metamodel.PluralAttribute;
import jakarta.persistence.metamodel.SingularAttribute; import jakarta.persistence.metamodel.SingularAttribute;
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@ -225,21 +227,37 @@ public abstract class AbstractSqmPath<T> extends AbstractSqmExpression<T> implem
* and if not creates a copy of the navigable path with the correct parent. * and if not creates a copy of the navigable path with the correct parent.
*/ */
protected NavigablePath getNavigablePathCopy(SqmPath<?> parent) { protected NavigablePath getNavigablePathCopy(SqmPath<?> parent) {
final NavigablePath parentNavigablePath = NullnessUtil.castNonNull( navigablePath.getRealParent() ); return getNavigablePathCopy( castNonNull( navigablePath.getRealParent() ), parent.getNavigablePath(), false, null );
if ( parent.getNavigablePath() == parentNavigablePath ) { }
private NavigablePath getNavigablePathCopy(
NavigablePath realParent,
NavigablePath parent,
boolean isId,
String identifierAttributeName) {
if ( parent == realParent ) {
return navigablePath; return navigablePath;
} }
else if ( CollectionPart.Nature.fromNameExact( parentNavigablePath.getLocalName() ) != null ) { else if ( !isId && realParent instanceof EntityIdentifierNavigablePath ) {
if ( parent.getNavigablePath() == parentNavigablePath.getRealParent() ) { return getNavigablePathCopy(
castNonNull( realParent.getRealParent() ),
parent,
true,
( (EntityIdentifierNavigablePath) realParent ).getIdentifierAttributeName()
);
}
else if ( CollectionPart.Nature.fromNameExact( realParent.getLocalName() ) != null ) {
if ( parent == realParent.getRealParent() ) {
return navigablePath; return navigablePath;
} }
else { else {
return parent.getNavigablePath() parent = parent.append( realParent.getLocalName() );
.append( parentNavigablePath.getLocalName() )
.append( navigablePath.getLocalName(), navigablePath.getAlias() );
} }
} }
return parent.getNavigablePath().append( navigablePath.getLocalName(), navigablePath.getAlias() ); if ( isId ) {
parent = new EntityIdentifierNavigablePath( parent, identifierAttributeName );
}
return parent.append( navigablePath.getLocalName(), navigablePath.getAlias() );
} }
@Override @Override