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 bc28b31202..03aba6fa1d 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 @@ -5810,8 +5810,7 @@ public abstract class AbstractEntityPersister private ModelPart getIdentifierModelPart(String name, EntityMappingType treatTargetType) { final EntityIdentifierMapping identifierMapping = getIdentifierMappingForJoin(); - if ( identifierMapping instanceof NonAggregatedIdentifierMapping ) { - NonAggregatedIdentifierMapping mapping = (NonAggregatedIdentifierMapping) identifierMapping; + if ( identifierMapping instanceof final NonAggregatedIdentifierMapping mapping ) { final ModelPart subPart = mapping.findSubPart( name, treatTargetType ); if ( subPart != null ) { return subPart; 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 add21d46c2..a7b1f3569b 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 @@ -3022,10 +3022,20 @@ public abstract class BaseSqmToSqlAstConverter extends Base parentType.getRootEntityDescriptor().getEntityName() ); } + final EntityDiscriminatorMapping discriminator = parentType.getDiscriminatorMapping(); + final String entityName; + if ( discriminator != null && discriminator.hasPhysicalColumn() && !parentType.getSubMappingTypes().isEmpty() ) { + // This is needed to preserve optimization for joined + discriminator inheritance + // see JoinedSubclassEntityPersister#getIdentifierMappingForJoin + entityName = parentType.getRootEntityDescriptor().getEntityName(); + } + else { + entityName = parentType.getEntityName(); + } registerEntityNameUsage( tableGroup, EntityNameUse.EXPRESSION, - parentType.getEntityName() + entityName ); } else {