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.
This commit is contained in:
Marco Belladelli 2023-08-02 15:31:36 +02:00
parent 9029d76604
commit b7ba6e731e
5 changed files with 23 additions and 8 deletions

View File

@ -299,6 +299,9 @@ public class OneToManyPersister extends AbstractCollectionPersister {
SqlAstCreationState astCreationState) { SqlAstCreationState astCreationState) {
super.applyWhereFragments( predicateConsumer, alias, tableGroup, astCreationState ); super.applyWhereFragments( 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( getElementPersisterInternal().applyDiscriminator(
predicateConsumer, predicateConsumer,
alias, alias,
@ -306,6 +309,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
astCreationState astCreationState
); );
} }
}
@Override @Override
public FilterAliasGenerator getFilterAliasGenerator(String rootAlias) { public FilterAliasGenerator getFilterAliasGenerator(String rootAlias) {

View File

@ -14,6 +14,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.BitSet; import java.util.BitSet;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
@ -3104,7 +3105,7 @@ public abstract class AbstractEntityPersister
TableGroup tableGroup, TableGroup tableGroup,
SqlAstCreationState creationState) { SqlAstCreationState creationState) {
if ( needsDiscriminator() ) { if ( needsDiscriminator() ) {
predicateConsumer.accept( createDiscriminatorPredicate( alias, tableGroup, creationState ) ); pruneForSubclasses( tableGroup, Collections.singletonMap( getEntityName(), EntityNameUse.TREAT ) );
} }
} }

View File

@ -3046,6 +3046,11 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
} }
} }
@Override
public boolean supportsEntityNameUsage() {
return true;
}
@Override @Override
public void registerEntityNameUsage( public void registerEntityNameUsage(
TableGroup tableGroup, TableGroup tableGroup,

View File

@ -42,4 +42,9 @@ public interface SqlAstCreationState {
String hibernateEntityName) { String hibernateEntityName) {
// No-op // No-op
} }
@Internal
default boolean supportsEntityNameUsage() {
return false;
}
} }

View File

@ -72,7 +72,7 @@ public class NamedTableReference extends AbstractTableReference {
@Override @Override
public boolean containsAffectedTableName(String requestedName) { public boolean containsAffectedTableName(String requestedName) {
return isEmpty( requestedName ) || getTableExpression().equals( requestedName ); return isEmpty( requestedName ) || getTableExpression().contains( requestedName );
} }
@Override @Override