Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2999,7 +2999,7 @@ protected void logStaticSQL() {

public abstract Map<Object, String> getSubclassByDiscriminatorValue();

protected abstract boolean needsDiscriminator();
public abstract boolean needsDiscriminator();

protected boolean isDiscriminatorFormula() {
return false;
Expand Down Expand Up @@ -3092,7 +3092,25 @@ public void applyDiscriminator(
TableGroup tableGroup,
SqlAstCreationState creationState) {
if ( needsDiscriminator() ) {
pruneForSubclasses( tableGroup, Collections.singletonMap( getEntityName(), EntityNameUse.TREAT ) );
assert !creationState.supportsEntityNameUsage() : "Entity name usage should have been used instead";
final Map<String, EntityNameUse> entityNameUseMap;
final Collection<EntityMappingType> subMappingTypes = getSubMappingTypes();
if ( subMappingTypes.isEmpty() ) {
entityNameUseMap = Collections.singletonMap( 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() );
}
}
pruneForSubclasses( tableGroup, entityNameUseMap );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ private void associateSubclassNamesToSubclassTableIndex(
}

@Override
protected boolean needsDiscriminator() {
public boolean needsDiscriminator() {
return forceDiscriminator;
}

Expand Down Expand Up @@ -1323,15 +1323,17 @@ public void pruneForSubclasses(TableGroup tableGroup, Map<String, EntityNameUse>
final String[] subclassTableNames = persister.getSubclassTableNames();
// Build the intersection of all tables names that are of the class or super class
// These are the tables that can be safely inner joined
if ( tablesToInnerJoin.isEmpty() ) {
for ( int i = 0; i < subclassTableNames.length; i++ ) {
if ( persister.isClassOrSuperclassTable[i] ) {
tablesToInnerJoin.add( subclassTableNames[i] );
}
final Set<String> classOrSuperclassTables = new HashSet<>( subclassTableNames.length );
for ( int i = 0; i < subclassTableNames.length; i++ ) {
if ( persister.isClassOrSuperclassTable[i] ) {
classOrSuperclassTables.add( subclassTableNames[i] );
}
}
if ( tablesToInnerJoin.isEmpty() ) {
tablesToInnerJoin.addAll( classOrSuperclassTables );
}
else {
tablesToInnerJoin.retainAll( Arrays.asList( subclassTableNames ) );
tablesToInnerJoin.retainAll( classOrSuperclassTables );
}
if ( useKind == EntityNameUse.UseKind.FILTER && explicitDiscriminatorColumnName == null ) {
// If there is no discriminator column,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ public String fromTableFragment(String name) {
}

@Override
protected boolean needsDiscriminator() {
public boolean needsDiscriminator() {
return forceDiscriminator || isInherited();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public TableGroup createRootTableGroup(
}

@Override
protected boolean needsDiscriminator() {
public boolean needsDiscriminator() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3118,20 +3118,30 @@ private void registerEntityNameUsage(
);
}
}
if ( useKind == EntityNameUse.UseKind.TREAT || useKind == EntityNameUse.UseKind.PROJECTION ) {
// If we encounter a treat use, we also want register the use for all subtypes.
// We do this here to not have to expand entity name uses during pruning later on
// If we encounter a treat or projection use, we also want register the use for all subtypes.
// We do this here to not have to expand entity name uses during pruning later on
if ( useKind == EntityNameUse.UseKind.TREAT ) {
for ( EntityMappingType subType : persister.getSubMappingTypes() ) {
entityNameUses.compute(
subType.getEntityName(),
(s, existingUse) -> finalEntityNameUse.stronger( existingUse )
);
if ( useKind == EntityNameUse.UseKind.PROJECTION ) {
actualTableGroup.resolveTableReference(
null,
subType.getEntityPersister().getMappedTableDetails().getTableName()
);
}
}
if ( persister.isInherited() && persister.needsDiscriminator() ) {
// Make sure the table group includes the root table when needed for TREAT
actualTableGroup.resolveTableReference( persister.getRootTableName() );
}
}
else if ( useKind == EntityNameUse.UseKind.PROJECTION ) {
for ( EntityMappingType subType : persister.getSubMappingTypes() ) {
entityNameUses.compute(
subType.getEntityName(),
(s, existingUse) -> finalEntityNameUse.stronger( existingUse )
);
actualTableGroup.resolveTableReference(
null,
subType.getEntityPersister().getMappedTableDetails().getTableName()
);
}
}
}
Expand Down Expand Up @@ -8238,7 +8248,9 @@ else if ( getLoadQueryInfluencers().hasEnabledFetchProfiles() ) {
if ( entityMappingType.getSuperMappingType() != null ) {
// A joined table group was created by an enabled entity graph or fetch profile,
// and it's of an inheritance subtype, so we should apply the discriminator
entityMappingType.applyDiscriminator( null, null, actualTableGroup, this );
getCurrentClauseStack().push( Clause.FROM );
registerEntityNameUsage( actualTableGroup, EntityNameUse.TREAT, entityMappingType.getEntityName() );
getCurrentClauseStack().pop();
}
}
}
Expand Down
Loading