Skip to content

Commit 7a227d7

Browse files
mbelladebeikov
authored andcommitted
HHH-17679 Fix unnecessary table reference resolution for treat
1 parent 28d8ec5 commit 7a227d7

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,10 @@ public void pruneForSubclasses(TableGroup tableGroup, Map<String, EntityNameUse>
14171417
applied = applyDiscriminatorPredicate( join, join.getJoinedTableReference(), entityNameUses, metamodel );
14181418
}
14191419
assert applied : "Could not apply treat discriminator predicate to root table join";
1420-
assert i == 0 || retainedTableReferences.contains( tableReferenceJoins.get( i - 1 ).getJoinedTableReference() );
1420+
if ( i != 0 ) {
1421+
// Always retain the root table reference join where the discriminator was applied
1422+
retainedTableReferences.add( tableReferenceJoins.get( i - 1 ).getJoinedTableReference() );
1423+
}
14211424
}
14221425
}
14231426
if ( tableReferenceJoins.isEmpty() ) {

hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,7 +3089,6 @@ private void registerEntityNameUsage(
30893089
actualTableGroup.resolveTableReference( null, persister.getTableName() );
30903090
}
30913091

3092-
final EntityNameUse.UseKind useKind = finalEntityNameUse.getKind();
30933092
if ( projection ) {
30943093
EntityMappingType superMappingType = persister;
30953094
while ( ( superMappingType = superMappingType.getSuperMappingType() ) != null ) {
@@ -3100,19 +3099,17 @@ private void registerEntityNameUsage(
31003099
);
31013100
}
31023101
}
3102+
31033103
// If we encounter a treat or projection use, we also want register the use for all subtypes.
31043104
// We do this here to not have to expand entity name uses during pruning later on
3105+
final EntityNameUse.UseKind useKind = finalEntityNameUse.getKind();
31053106
if ( useKind == EntityNameUse.UseKind.TREAT ) {
31063107
for ( EntityMappingType subType : persister.getSubMappingTypes() ) {
31073108
entityNameUses.compute(
31083109
subType.getEntityName(),
31093110
(s, existingUse) -> finalEntityNameUse.stronger( existingUse )
31103111
);
31113112
}
3112-
if ( persister.isInherited() && persister.needsDiscriminator() ) {
3113-
// Make sure the table group includes the root table when needed for TREAT
3114-
actualTableGroup.resolveTableReference( persister.getRootTableName() );
3115-
}
31163113
}
31173114
else if ( useKind == EntityNameUse.UseKind.PROJECTION ) {
31183115
for ( EntityMappingType subType : persister.getSubMappingTypes() ) {
@@ -3358,7 +3355,7 @@ private TableGroup consumeAttributeJoin(
33583355
// This is a non-treated join with an entity which is an inheritance subtype,
33593356
// register a TREAT entity name use to filter only the entities of the correct type.
33603357
registerEntityNameUsage(
3361-
getActualTableGroup( joinedTableGroup, sqmJoin ),
3358+
elementTableGroup,
33623359
EntityNameUse.TREAT,
33633360
entityDomainType.getHibernateEntityName()
33643361
);

0 commit comments

Comments
 (0)