|
93 | 93 | import org.hibernate.persister.entity.AbstractEntityPersister;
|
94 | 94 | import org.hibernate.persister.entity.EntityNameUse;
|
95 | 95 | import org.hibernate.persister.entity.EntityPersister;
|
96 |
| -import org.hibernate.persister.entity.SingleTableEntityPersister; |
97 | 96 | import org.hibernate.query.BindableType;
|
98 | 97 | import org.hibernate.query.QueryLogging;
|
99 | 98 | import org.hibernate.query.ReturnableType;
|
@@ -3167,9 +3166,8 @@ protected void registerTypeUsage(TableGroup tableGroup) {
|
3167 | 3166 | registerEntityNameUsage( tableGroup, EntityNameUse.PROJECTION, persister.getEntityName(), true );
|
3168 | 3167 | }
|
3169 | 3168 | else {
|
3170 |
| - // Avoid doing this for single table entity persisters, as the table span includes secondary tables, |
3171 |
| - // which we don't want to resolve, though we know that there is only a single table anyway |
3172 |
| - if ( persister instanceof SingleTableEntityPersister ) { |
| 3169 | + // Avoid resolving subclass tables for persisters with physical discriminators as we won't need them |
| 3170 | + if ( persister.getDiscriminatorMapping().hasPhysicalColumn() ) { |
3173 | 3171 | return;
|
3174 | 3172 | }
|
3175 | 3173 | final int subclassTableSpan = persister.getSubclassTableSpan();
|
@@ -7557,46 +7555,56 @@ else if ( rhs instanceof DiscriminatorPathInterpretation ) {
|
7557 | 7555 | else {
|
7558 | 7556 | return;
|
7559 | 7557 | }
|
7560 |
| - if ( literalExpression == null ) { |
7561 |
| - // We have to assume all types are possible and can't do optimizations |
7562 |
| - final TableGroup tableGroup = getFromClauseIndex().getTableGroup( typeExpression.getNavigablePath().getParent() ); |
7563 |
| - final EntityMappingType entityMappingType = (EntityMappingType) tableGroup.getModelPart().getPartMappingType(); |
7564 |
| - registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() ); |
7565 |
| - for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) { |
7566 |
| - registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() ); |
7567 |
| - } |
7568 |
| - } |
7569 |
| - else { |
7570 |
| - handleTypeComparison( typeExpression, Collections.singletonList( literalExpression ), inclusive ); |
7571 |
| - } |
| 7558 | + handleTypeComparison( |
| 7559 | + typeExpression, |
| 7560 | + literalExpression != null ? singletonList( literalExpression ) : null, |
| 7561 | + inclusive |
| 7562 | + ); |
7572 | 7563 | }
|
7573 | 7564 |
|
7574 | 7565 | private void handleTypeComparison(
|
7575 |
| - DiscriminatorPathInterpretation typeExpression, |
| 7566 | + DiscriminatorPathInterpretation<?> typeExpression, |
7576 | 7567 | List<EntityTypeLiteral> literalExpressions,
|
7577 | 7568 | boolean inclusive) {
|
7578 | 7569 | final TableGroup tableGroup = getFromClauseIndex().getTableGroup( typeExpression.getNavigablePath().getParent() );
|
7579 |
| - if ( inclusive ) { |
7580 |
| - for ( EntityTypeLiteral literalExpr : literalExpressions ) { |
7581 |
| - registerEntityNameUsage( |
7582 |
| - tableGroup, |
7583 |
| - EntityNameUse.FILTER, |
7584 |
| - literalExpr.getEntityTypeDescriptor().getEntityName() |
7585 |
| - ); |
| 7570 | + final EntityMappingType entityMappingType = (EntityMappingType) tableGroup.getModelPart().getPartMappingType(); |
| 7571 | + if ( entityMappingType.getDiscriminatorMapping().hasPhysicalColumn() ) { |
| 7572 | + // Prevent pruning of the root type's table reference containing the physical discriminator column |
| 7573 | + registerEntityNameUsage( |
| 7574 | + tableGroup, |
| 7575 | + EntityNameUse.EXPRESSION, |
| 7576 | + entityMappingType.getRootEntityDescriptor().getEntityName() |
| 7577 | + ); |
| 7578 | + } |
| 7579 | + if ( literalExpressions == null ) { |
| 7580 | + // We have to assume all types are possible and can't do optimizations |
| 7581 | + registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() ); |
| 7582 | + for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) { |
| 7583 | + registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() ); |
7586 | 7584 | }
|
7587 | 7585 | }
|
7588 | 7586 | else {
|
7589 |
| - final EntityMappingType entityMappingType = (EntityMappingType) tableGroup.getModelPart().getPartMappingType(); |
7590 |
| - final Set<String> excludedEntityNames = new HashSet<>(entityMappingType.getSubMappingTypes().size()); |
7591 |
| - for ( EntityTypeLiteral literalExpr : literalExpressions ) { |
7592 |
| - excludedEntityNames.add( literalExpr.getEntityTypeDescriptor().getEntityName() ); |
7593 |
| - } |
7594 |
| - if ( !excludedEntityNames.contains( entityMappingType.getEntityName() ) ) { |
7595 |
| - registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() ); |
| 7587 | + if ( inclusive ) { |
| 7588 | + for ( EntityTypeLiteral literalExpr : literalExpressions ) { |
| 7589 | + registerEntityNameUsage( |
| 7590 | + tableGroup, |
| 7591 | + EntityNameUse.FILTER, |
| 7592 | + literalExpr.getEntityTypeDescriptor().getEntityName() |
| 7593 | + ); |
| 7594 | + } |
7596 | 7595 | }
|
7597 |
| - for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) { |
7598 |
| - if ( !excludedEntityNames.contains( subMappingType.getEntityName() ) ) { |
7599 |
| - registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() ); |
| 7596 | + else { |
| 7597 | + final Set<String> excludedEntityNames = new HashSet<>( entityMappingType.getSubMappingTypes().size() ); |
| 7598 | + for ( EntityTypeLiteral literalExpr : literalExpressions ) { |
| 7599 | + excludedEntityNames.add( literalExpr.getEntityTypeDescriptor().getEntityName() ); |
| 7600 | + } |
| 7601 | + if ( !excludedEntityNames.contains( entityMappingType.getEntityName() ) ) { |
| 7602 | + registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() ); |
| 7603 | + } |
| 7604 | + for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) { |
| 7605 | + if ( !excludedEntityNames.contains( subMappingType.getEntityName() ) ) { |
| 7606 | + registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() ); |
| 7607 | + } |
7600 | 7608 | }
|
7601 | 7609 | }
|
7602 | 7610 | }
|
@@ -7852,26 +7860,12 @@ private void handleTypeComparison(InListPredicate inPredicate) {
|
7852 | 7860 | break;
|
7853 | 7861 | }
|
7854 | 7862 | }
|
7855 |
| - if ( containsNonLiteral ) { |
7856 |
| - // We have to assume all types are possible and can't do optimizations |
7857 |
| - final TableGroup tableGroup = getFromClauseIndex().getTableGroup( |
7858 |
| - typeExpression.getNavigablePath().getParent() |
7859 |
| - ); |
7860 |
| - final EntityMappingType entityMappingType = (EntityMappingType) tableGroup.getModelPart() |
7861 |
| - .getPartMappingType(); |
7862 |
| - registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() ); |
7863 |
| - for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) { |
7864 |
| - registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() ); |
7865 |
| - } |
7866 |
| - } |
7867 |
| - else { |
7868 |
| - //noinspection unchecked |
7869 |
| - handleTypeComparison( |
7870 |
| - typeExpression, |
7871 |
| - (List<EntityTypeLiteral>) (List<?>) inPredicate.getListExpressions(), |
7872 |
| - !inPredicate.isNegated() |
7873 |
| - ); |
7874 |
| - } |
| 7863 | + //noinspection unchecked |
| 7864 | + handleTypeComparison( |
| 7865 | + typeExpression, |
| 7866 | + containsNonLiteral ? null : (List<EntityTypeLiteral>) (List<?>) inPredicate.getListExpressions(), |
| 7867 | + !inPredicate.isNegated() |
| 7868 | + ); |
7875 | 7869 | }
|
7876 | 7870 | }
|
7877 | 7871 |
|
|
0 commit comments