HHH-17113 Fix joined inheritance and force discriminator pruning

This commit is contained in:
Marco Belladelli 2024-01-24 16:27:01 +01:00 committed by Christian Beikov
parent ebdd267a9e
commit 7686b6063b
2 changed files with 12 additions and 9 deletions

View File

@ -3099,20 +3099,21 @@ public abstract class AbstractEntityPersister
assert !creationState.supportsEntityNameUsage() : "Entity name usage should have been used instead";
final Map<String, EntityNameUse> entityNameUseMap;
final Collection<EntityMappingType> subMappingTypes = getSubMappingTypes();
entityNameUseMap = new HashMap<>( 1 + subMappingTypes.size() + ( isInherited() ? 1 : 0 ) );
if ( subMappingTypes.isEmpty() ) {
entityNameUseMap = Collections.singletonMap( getEntityName(), EntityNameUse.TREAT );
entityNameUseMap.put( getEntityName(), EntityNameUse.TREAT );
}
else {
entityNameUseMap = new HashMap<>( 1 + subMappingTypes.size() );
entityNameUseMap.put( getEntityName(), EntityNameUse.TREAT );
// We need to register TREAT uses for all subtypes when pruning
for ( EntityMappingType subMappingType : subMappingTypes ) {
entityNameUseMap.put( subMappingType.getEntityName(), EntityNameUse.TREAT );
}
if ( isInherited() ) {
// Make sure the table group includes the root table when needed for TREAT
tableGroup.resolveTableReference( getRootTableName() );
}
}
if ( isInherited() ) {
// Make sure the table group includes the root table when needed for TREAT
tableGroup.resolveTableReference( getRootTableName() );
entityNameUseMap.put( getRootEntityName(), EntityNameUse.EXPRESSION );
}
pruneForSubclasses( tableGroup, entityNameUseMap );
}

View File

@ -1377,8 +1377,8 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
// We allow multiple joined subclasses to use the same table if they define a discriminator column.
// In this case, we might need to add a discriminator condition to make sure we filter the correct subtype,
// see SingleTableEntityPersister#pruneForSubclasses for more details on this condition
needsTreatDiscriminator = needsTreatDiscriminator || !persister.isAbstract() &&
!isTypeOrSuperType( persister ) && useKind == EntityNameUse.UseKind.TREAT;
needsTreatDiscriminator = needsTreatDiscriminator || !persister.isAbstract()
&& useKind == EntityNameUse.UseKind.TREAT && ( isInherited() || !isTypeOrSuperType( persister ) );
}
}
// If no tables to inner join have been found, we add at least the super class tables of this persister
@ -1409,11 +1409,13 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
entityNameUses,
metamodel
);
for ( int i = 0; !applied && i < tableReferenceJoins.size(); i++ ) {
int i = 0;
for ( ; !applied && i < tableReferenceJoins.size(); i++ ) {
final TableReferenceJoin join = tableReferenceJoins.get( i );
applied = applyDiscriminatorPredicate( join, join.getJoinedTableReference(), entityNameUses, metamodel );
}
assert applied : "Could not apply treat discriminator predicate to root table join";
assert i == 0 || retainedTableReferences.contains( tableReferenceJoins.get( i - 1 ).getJoinedTableReference() );
}
}
if ( tableReferenceJoins.isEmpty() ) {