From b7ba6e731e1e2090cadeec0a9219a6410cc0cb01 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 2 Aug 2023 15:31:36 +0200 Subject: [PATCH] HHH-16616 Apply discriminator by pruning table group Also, only apply the discriminator for loads since in queries the pruning is already handled by the entity name usage logic. --- .../persister/collection/OneToManyPersister.java | 16 ++++++++++------ .../entity/AbstractEntityPersister.java | 3 ++- .../query/sqm/sql/BaseSqmToSqlAstConverter.java | 5 +++++ .../sql/ast/spi/SqlAstCreationState.java | 5 +++++ .../sql/ast/tree/from/NamedTableReference.java | 2 +- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java index b3268a09d2..dd58ec415a 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java @@ -299,12 +299,16 @@ public class OneToManyPersister extends AbstractCollectionPersister { SqlAstCreationState astCreationState) { super.applyWhereFragments( predicateConsumer, alias, tableGroup, astCreationState ); - getElementPersisterInternal().applyDiscriminator( - predicateConsumer, - alias, - tableGroup, - astCreationState - ); + if ( !astCreationState.supportsEntityNameUsage() ) { + // We only need to apply discriminator for loads, since queries with joined + // inheritance subtypes are already filtered by the entity name usage logic + getElementPersisterInternal().applyDiscriminator( + predicateConsumer, + alias, + tableGroup, + astCreationState + ); + } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index eebcbad6c4..895a5da7c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.IdentityHashMap; @@ -3104,7 +3105,7 @@ public abstract class AbstractEntityPersister TableGroup tableGroup, SqlAstCreationState creationState) { if ( needsDiscriminator() ) { - predicateConsumer.accept( createDiscriminatorPredicate( alias, tableGroup, creationState ) ); + pruneForSubclasses( tableGroup, Collections.singletonMap( getEntityName(), EntityNameUse.TREAT ) ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index 2fde605f81..0ccd9461e6 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -3046,6 +3046,11 @@ public abstract class BaseSqmToSqlAstConverter extends Base } } + @Override + public boolean supportsEntityNameUsage() { + return true; + } + @Override public void registerEntityNameUsage( TableGroup tableGroup, diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationState.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationState.java index 89a46ff154..5a599ad10e 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationState.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationState.java @@ -42,4 +42,9 @@ public interface SqlAstCreationState { String hibernateEntityName) { // No-op } + + @Internal + default boolean supportsEntityNameUsage() { + return false; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/NamedTableReference.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/NamedTableReference.java index 33e4dc2f62..d78c1a3c7e 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/NamedTableReference.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/NamedTableReference.java @@ -72,7 +72,7 @@ public class NamedTableReference extends AbstractTableReference { @Override public boolean containsAffectedTableName(String requestedName) { - return isEmpty( requestedName ) || getTableExpression().equals( requestedName ); + return isEmpty( requestedName ) || getTableExpression().contains( requestedName ); } @Override