Skip to content

Commit bfa1a86

Browse files
mbelladebeikov
authored andcommitted
HHH-17113 Fix joined inheritance and force discriminator pruning
1 parent b97cb37 commit bfa1a86

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,20 +3137,21 @@ public void applyDiscriminator(
31373137
assert !creationState.supportsEntityNameUsage() : "Entity name usage should have been used instead";
31383138
final Map<String, EntityNameUse> entityNameUseMap;
31393139
final Collection<EntityMappingType> subMappingTypes = getSubMappingTypes();
3140+
entityNameUseMap = new HashMap<>( 1 + subMappingTypes.size() + ( isInherited() ? 1 : 0 ) );
31403141
if ( subMappingTypes.isEmpty() ) {
3141-
entityNameUseMap = Collections.singletonMap( getEntityName(), EntityNameUse.TREAT );
3142+
entityNameUseMap.put( getEntityName(), EntityNameUse.TREAT );
31423143
}
31433144
else {
3144-
entityNameUseMap = new HashMap<>( 1 + subMappingTypes.size() );
31453145
entityNameUseMap.put( getEntityName(), EntityNameUse.TREAT );
31463146
// We need to register TREAT uses for all subtypes when pruning
31473147
for ( EntityMappingType subMappingType : subMappingTypes ) {
31483148
entityNameUseMap.put( subMappingType.getEntityName(), EntityNameUse.TREAT );
31493149
}
3150-
if ( isInherited() ) {
3151-
// Make sure the table group includes the root table when needed for TREAT
3152-
tableGroup.resolveTableReference( getRootTableName() );
3153-
}
3150+
}
3151+
if ( isInherited() ) {
3152+
// Make sure the table group includes the root table when needed for TREAT
3153+
tableGroup.resolveTableReference( getRootTableName() );
3154+
entityNameUseMap.put( getRootEntityName(), EntityNameUse.EXPRESSION );
31543155
}
31553156
pruneForSubclasses( tableGroup, entityNameUseMap );
31563157
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,8 +1379,8 @@ public void pruneForSubclasses(TableGroup tableGroup, Map<String, EntityNameUse>
13791379
// We allow multiple joined subclasses to use the same table if they define a discriminator column.
13801380
// In this case, we might need to add a discriminator condition to make sure we filter the correct subtype,
13811381
// see SingleTableEntityPersister#pruneForSubclasses for more details on this condition
1382-
needsTreatDiscriminator = needsTreatDiscriminator || !persister.isAbstract() &&
1383-
!isTypeOrSuperType( persister ) && useKind == EntityNameUse.UseKind.TREAT;
1382+
needsTreatDiscriminator = needsTreatDiscriminator || !persister.isAbstract()
1383+
&& useKind == EntityNameUse.UseKind.TREAT && ( isInherited() || !isTypeOrSuperType( persister ) );
13841384
}
13851385
}
13861386
// If no tables to inner join have been found, we add at least the super class tables of this persister
@@ -1411,11 +1411,13 @@ public void pruneForSubclasses(TableGroup tableGroup, Map<String, EntityNameUse>
14111411
entityNameUses,
14121412
metamodel
14131413
);
1414-
for ( int i = 0; !applied && i < tableReferenceJoins.size(); i++ ) {
1414+
int i = 0;
1415+
for ( ; !applied && i < tableReferenceJoins.size(); i++ ) {
14151416
final TableReferenceJoin join = tableReferenceJoins.get( i );
14161417
applied = applyDiscriminatorPredicate( join, join.getJoinedTableReference(), entityNameUses, metamodel );
14171418
}
14181419
assert applied : "Could not apply treat discriminator predicate to root table join";
1420+
assert i == 0 || retainedTableReferences.contains( tableReferenceJoins.get( i - 1 ).getJoinedTableReference() );
14191421
}
14201422
}
14211423
if ( tableReferenceJoins.isEmpty() ) {

0 commit comments

Comments
 (0)