PluralAttributeMapping, add associate primary table join only when necessary

This commit is contained in:
Andrea Boriero 2020-08-06 10:31:33 +01:00
parent bdc1130f00
commit bd3775b114
2 changed files with 40 additions and 27 deletions

View File

@ -8,6 +8,7 @@ package org.hibernate.metamodel.mapping.internal;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.hibernate.LockMode;
@ -696,7 +697,7 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping
creationContext.getSessionFactory()
);
final Consumer<TableGroup> tableGroupFinalizer;
final BiFunction<String, TableGroup, TableReferenceJoin> tableReferenceJoinCreator;
final java.util.function.Predicate<String> tableReferenceJoinNameChecker;
if ( elementDescriptor instanceof EntityCollectionPart || indexDescriptor instanceof EntityCollectionPart ) {
@ -715,19 +716,11 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping
creationContext
);
tableReferenceJoinNameChecker = mappingType::containsTableReference;
tableReferenceJoinCreator = (tableExpression, tableGroup) -> mappingType.createTableReferenceJoin(
tableExpression,
sqlAliasBase,
associatedPrimaryTable,
canUseInnerJoin && !getAttributeMetadataAccess().resolveAttributeMetadata( null ).isNullable(),
sqlExpressionResolver,
creationContext
);
final boolean useInnerJoin = canUseInnerJoin && !getAttributeMetadataAccess()
.resolveAttributeMetadata( null ).isNullable();
tableGroupFinalizer = tableGroup -> {
final SqlAstJoinType joinType = canUseInnerJoin && !getAttributeMetadataAccess().resolveAttributeMetadata(
null ).isNullable()
final Function<TableGroup,TableReferenceJoin> tableGroupFinalizer = tableGroup -> {
final SqlAstJoinType joinType = useInnerJoin
? SqlAstJoinType.INNER
: SqlAstJoinType.LEFT;
final TableReferenceJoin associationJoin = new TableReferenceJoin(
@ -741,7 +734,31 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping
creationContext
)
);
( (StandardTableGroup) tableGroup ).addTableReferenceJoin( associationJoin );
return associationJoin;
};
tableReferenceJoinNameChecker = mappingType::containsTableReference;
tableReferenceJoinCreator = (tableExpression, tableGroup) -> {
if ( associatedPrimaryTable.getTableExpression().equals( tableExpression ) ) {
TableReferenceJoin tableReferenceJoin = tableGroupFinalizer.apply( tableGroup );
return tableReferenceJoin;
}
else {
StandardTableGroup standardTableGroup = (StandardTableGroup) tableGroup;
if ( standardTableGroup.getTableReferenceJoins().isEmpty() ) {
TableReferenceJoin tableReferenceJoin = tableGroupFinalizer.apply( tableGroup );
standardTableGroup.addTableReferenceJoin( tableReferenceJoin );
}
}
return mappingType.createTableReferenceJoin(
tableExpression,
sqlAliasBase,
associatedPrimaryTable,
useInnerJoin,
sqlExpressionResolver,
creationContext
);
};
}
else {
@ -751,7 +768,6 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping
);
};
tableReferenceJoinNameChecker = s -> false;
tableGroupFinalizer = null;
}
final StandardTableGroup tableGroup = new StandardTableGroup(
@ -765,13 +781,10 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping
creationContext.getSessionFactory()
);
if ( tableGroupFinalizer != null ) {
tableGroupFinalizer.accept( tableGroup );
}
return tableGroup;
}
@Override
public TableGroup createRootTableGroup(
NavigablePath navigablePath,

View File

@ -75,6 +75,11 @@ public class StandardTableGroup extends AbstractTableGroup {
}
}
@Override
public TableReference getTableReference(String tableExpression) {
return getTableReferenceInternal( tableExpression );
}
@Override
public TableReference getPrimaryTableReference() {
return primaryTableReference;
@ -89,13 +94,12 @@ public class StandardTableGroup extends AbstractTableGroup {
if ( tableJoins == null ) {
tableJoins = new ArrayList<>();
}
tableJoins.add( join );
}
@Override
public TableReference getTableReferenceInternal(String tableExpression) {
final TableReference tableReference = super.getTableReferenceInternal( tableExpression );
TableReference tableReference = primaryTableReference.getTableReference( tableExpression );
if ( tableReference != null ) {
return tableReference;
}
@ -105,7 +109,7 @@ public class StandardTableGroup extends AbstractTableGroup {
for ( int i = 0; i < tableJoins.size(); i++ ) {
final TableReferenceJoin join = tableJoins.get( i );
assert join != null;
if ( join.getJoinedTableReference().getTableReference( tableExpression ) != null ) {
if ( join.getJoinedTableReference().getTableExpression().equals( tableExpression ) ) {
return join.getJoinedTableReference();
}
}
@ -128,13 +132,9 @@ public class StandardTableGroup extends AbstractTableGroup {
protected TableReference potentiallyCreateTableReference(String tableExpression) {
final TableReferenceJoin join = tableReferenceJoinCreator.apply( tableExpression, this );
if ( join != null ) {
if ( tableJoins == null ) {
tableJoins = new ArrayList<>();
}
tableJoins.add( join );
addTableReferenceJoin( join );
return join.getJoinedTableReference();
}
return null;
}
}