BaseSqmToSqlAstConverter#consumeAttributeJoin() code clean up

This commit is contained in:
Andrea Boriero 2020-12-10 14:34:14 +01:00
parent 6d2e80d43a
commit 820b815b35
1 changed files with 49 additions and 114 deletions

View File

@ -19,7 +19,6 @@ import java.util.function.Supplier;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.NotYetImplementedFor6Exception; import org.hibernate.NotYetImplementedFor6Exception;
@ -43,7 +42,6 @@ import org.hibernate.metamodel.model.domain.AllowableParameterType;
import org.hibernate.metamodel.model.domain.EntityDomainType; import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.metamodel.model.domain.PluralPersistentAttribute; import org.hibernate.metamodel.model.domain.PluralPersistentAttribute;
import org.hibernate.metamodel.model.domain.internal.CompositeSqmPathSource; import org.hibernate.metamodel.model.domain.internal.CompositeSqmPathSource;
import org.hibernate.metamodel.model.domain.internal.EmbeddedSqmPathSource;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.BinaryArithmeticOperator; import org.hibernate.query.BinaryArithmeticOperator;
import org.hibernate.query.NavigablePath; import org.hibernate.query.NavigablePath;
@ -250,7 +248,7 @@ public abstract class BaseSqmToSqlAstConverter
private final QueryParameterBindings domainParameterBindings; private final QueryParameterBindings domainParameterBindings;
private final Map<JpaCriteriaParameter<?>,Supplier<SqmJpaCriteriaParameterWrapper<?>>> jpaCriteriaParamResolutions; private final Map<JpaCriteriaParameter<?>,Supplier<SqmJpaCriteriaParameterWrapper<?>>> jpaCriteriaParamResolutions;
private Map<String,NavigablePath> pluralPersisterElementNavigablePathByFullPath = new HashMap<>(); private Map<String,NavigablePath> joinPathBySqmJoinFullPath = new HashMap<>();
private final SqlAliasBaseManager sqlAliasBaseManager = new SqlAliasBaseManager(); private final SqlAliasBaseManager sqlAliasBaseManager = new SqlAliasBaseManager();
@ -485,6 +483,7 @@ public abstract class BaseSqmToSqlAstConverter
postProcessQuerySpec( sqlQuerySpec ); postProcessQuerySpec( sqlQuerySpec );
joinPathBySqmJoinFullPath.clear();
return sqlQuerySpec; return sqlQuerySpec;
} }
finally { finally {
@ -676,11 +675,8 @@ public abstract class BaseSqmToSqlAstConverter
log.tracef( "Visiting explicit joins for `%s`", sqmFrom.getNavigablePath() ); log.tracef( "Visiting explicit joins for `%s`", sqmFrom.getNavigablePath() );
} }
sqmFrom.visitSqmJoins( sqmFrom.visitSqmJoins(
sqmJoin -> { sqmJoin -> consumeExplicitJoin( sqmJoin, lhsTableGroup )
consumeExplicitJoin( sqmJoin, lhsTableGroup );
}
); );
pluralPersisterElementNavigablePathByFullPath.clear();
} }
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
@ -708,135 +704,57 @@ public abstract class BaseSqmToSqlAstConverter
final NavigablePath sqmJoinNavigablePath = sqmJoin.getNavigablePath(); final NavigablePath sqmJoinNavigablePath = sqmJoin.getNavigablePath();
final NavigablePath parentNavigablePath = sqmJoinNavigablePath.getParent(); final NavigablePath parentNavigablePath = sqmJoinNavigablePath.getParent();
if ( pathSource instanceof PluralPersistentAttribute ) {
final ModelPart pluralPart = lhsTableGroup.getModelPart().findSubPart( final ModelPart modelPart = lhsTableGroup.getModelPart().findSubPart(
pathSource.getPathName(), pathSource.getPathName(),
SqmMappingModelHelper.resolveExplicitTreatTarget( sqmJoin, this ) SqmMappingModelHelper.resolveExplicitTreatTarget( sqmJoin, this )
); );
assert pluralPart instanceof PluralAttributeMapping; final NavigablePath joinPath;
if ( pathSource instanceof PluralPersistentAttribute ) {
assert modelPart instanceof PluralAttributeMapping;
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) pluralPart; final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) modelPart;
NavigablePath elementPath; joinPath = getJoinNavigablePath(
if ( parentNavigablePath == null ) { sqmJoinNavigablePath,
elementPath = sqmJoinNavigablePath; parentNavigablePath,
pluralPersisterElementNavigablePathByFullPath.put( pluralAttributeMapping.getPartName()
sqmJoin.getNavigablePath().getFullPath(), );
elementPath
joinPathBySqmJoinFullPath.put(
sqmJoin.getNavigablePath().getFullPath(),
joinPath.append( CollectionPart.Nature.ELEMENT.getName() )
); );
}
else {
final NavigablePath elementNavigablePath = pluralPersisterElementNavigablePathByFullPath.get(
parentNavigablePath.getFullPath() );
if ( elementNavigablePath == null ) {
elementPath = sqmJoinNavigablePath;
}
else {
elementPath = elementNavigablePath.append( pluralAttributeMapping.getPartName() );
}
}
pluralPersisterElementNavigablePathByFullPath.put( sqmJoin.getNavigablePath().getFullPath(), elementPath.append( CollectionPart.Nature.ELEMENT.getName() ) );
joinedTableGroupJoin = pluralAttributeMapping.createTableGroupJoin( joinedTableGroupJoin = pluralAttributeMapping.createTableGroupJoin(
elementPath, joinPath,
lhsTableGroup, lhsTableGroup,
sqmJoin.getExplicitAlias(), sqmJoin.getExplicitAlias(),
sqmJoin.getSqmJoinType().getCorrespondingSqlJoinType(), sqmJoin.getSqmJoinType().getCorrespondingSqlJoinType(),
determineLockMode( sqmJoin.getExplicitAlias() ), determineLockMode( sqmJoin.getExplicitAlias() ),
this this
); );
joinedTableGroup = joinedTableGroupJoin.getJoinedGroup();
lhsTableGroup.addTableGroupJoin( joinedTableGroupJoin );
fromClauseIndex.register( sqmJoin, joinedTableGroup, elementPath );
// fromClauseIndex.registerTableGroup( elementPath, joinedTableGroup );
}
else if ( pathSource instanceof EmbeddedSqmPathSource ) {
final ModelPart joinedPart = lhsTableGroup.getModelPart().findSubPart(
pathSource.getPathName(),
SqmMappingModelHelper.resolveExplicitTreatTarget( sqmJoin, this )
);
assert joinedPart instanceof TableGroupJoinProducer;
final NavigablePath joinedPath;
final String explicitAlias = sqmJoin.getExplicitAlias();
if ( explicitAlias == null ) {
joinedPath = sqmJoinNavigablePath;
} }
else { else {
joinedPath = parentNavigablePath.append( sqmJoin.getAttribute().getName() ); assert modelPart instanceof TableGroupJoinProducer;
}
joinedTableGroupJoin = ( (TableGroupJoinProducer) joinedPart ).createTableGroupJoin( joinPath = getJoinNavigablePath( sqmJoinNavigablePath, parentNavigablePath, modelPart.getPartName() );
joinedPath,
joinedTableGroupJoin = ( (TableGroupJoinProducer) modelPart ).createTableGroupJoin(
joinPath,
lhsTableGroup, lhsTableGroup,
sqmJoin.getExplicitAlias(), sqmJoin.getExplicitAlias(),
sqmJoin.getSqmJoinType().getCorrespondingSqlJoinType(), sqmJoin.getSqmJoinType().getCorrespondingSqlJoinType(),
determineLockMode( sqmJoin.getExplicitAlias() ), determineLockMode( sqmJoin.getExplicitAlias() ),
this this
); );
}
joinedTableGroup = joinedTableGroupJoin.getJoinedGroup(); joinedTableGroup = joinedTableGroupJoin.getJoinedGroup();
lhsTableGroup.addTableGroupJoin( joinedTableGroupJoin ); lhsTableGroup.addTableGroupJoin( joinedTableGroupJoin );
fromClauseIndex.register( sqmJoin, joinedTableGroup ); fromClauseIndex.register( sqmJoin, joinedTableGroup, joinPath );
}
else {
final ModelPart joinedPart = lhsTableGroup.getModelPart().findSubPart(
pathSource.getPathName(),
SqmMappingModelHelper.resolveExplicitTreatTarget( sqmJoin, this )
);
if ( !TableGroupJoinProducer.class.isInstance( joinedPart ) ) {
throw new HibernateException( "Expecting joined model part to implement TableGroupJoinProducer - " + joinedPart );
}
final NavigablePath joinedPath;
final String explicitAlias = sqmJoin.getExplicitAlias();
if ( explicitAlias == null ) {
joinedPath = sqmJoinNavigablePath;
}
else {
joinedPath = parentNavigablePath.append( sqmJoin.getAttribute().getName() );
}
NavigablePath elementPath;
if ( parentNavigablePath == null ) {
elementPath = sqmJoinNavigablePath;
pluralPersisterElementNavigablePathByFullPath.put(
sqmJoin.getNavigablePath().getFullPath(),
elementPath
);
}
else {
final NavigablePath elementNavigablePath = pluralPersisterElementNavigablePathByFullPath.get(
parentNavigablePath.getFullPath() );
if ( elementNavigablePath == null ) {
elementPath = sqmJoinNavigablePath;
}
else {
elementPath = elementNavigablePath.append( joinedPart.getPartName() );
}
}
joinedTableGroupJoin = ( (TableGroupJoinProducer) joinedPart ).createTableGroupJoin(
elementPath,
lhsTableGroup,
sqmJoin.getExplicitAlias(),
sqmJoin.getSqmJoinType().getCorrespondingSqlJoinType(),
determineLockMode( sqmJoin.getExplicitAlias() ),
this
);
joinedTableGroup = joinedTableGroupJoin.getJoinedGroup();
lhsTableGroup.addTableGroupJoin( joinedTableGroupJoin );
fromClauseIndex.register( sqmJoin, joinedTableGroup,elementPath );
}
// add any additional join restrictions // add any additional join restrictions
if ( sqmJoin.getJoinPredicate() != null ) { if ( sqmJoin.getJoinPredicate() != null ) {
@ -857,6 +775,23 @@ public abstract class BaseSqmToSqlAstConverter
consumeImplicitJoins( sqmJoin, joinedTableGroup ); consumeImplicitJoins( sqmJoin, joinedTableGroup );
} }
private NavigablePath getJoinNavigablePath(
NavigablePath sqmJoinNavigablePath,
NavigablePath parentNavigablePath, String partName) {
if ( parentNavigablePath == null ) {
return sqmJoinNavigablePath;
}
else {
final NavigablePath elementNavigablePath = joinPathBySqmJoinFullPath.get( parentNavigablePath.getFullPath() );
if ( elementNavigablePath == null ) {
return sqmJoinNavigablePath;
}
else {
return elementNavigablePath.append( partName );
}
}
}
private void consumeCrossJoin(SqmCrossJoin sqmJoin, TableGroup lhsTableGroup) { private void consumeCrossJoin(SqmCrossJoin sqmJoin, TableGroup lhsTableGroup) {
final EntityPersister entityDescriptor = resolveEntityPersister( sqmJoin.getReferencedPathSource() ); final EntityPersister entityDescriptor = resolveEntityPersister( sqmJoin.getReferencedPathSource() );