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

View File

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