Search in sources :

Example 1 with TableGroupProducer

use of org.hibernate.sql.ast.tree.from.TableGroupProducer in project hibernate-orm by hibernate.

the class ToOneAttributeMapping method createRootTableGroupJoin.

@Override
public LazyTableGroup createRootTableGroupJoin(NavigablePath navigablePath, TableGroup lhs, String explicitSourceAlias, SqlAstJoinType requestedJoinType, boolean fetched, Consumer<Predicate> predicateConsumer, SqlAliasBaseGenerator aliasBaseGenerator, SqlExpressionResolver sqlExpressionResolver, FromClauseAccess fromClauseAccess, SqlAstCreationContext creationContext) {
    final SqlAliasBase sqlAliasBase = aliasBaseGenerator.createSqlAliasBase(sqlAliasStem);
    final SqlAstJoinType joinType;
    if (requestedJoinType == null) {
        joinType = SqlAstJoinType.INNER;
    } else {
        joinType = requestedJoinType;
    }
    final boolean canUseInnerJoin = joinType == SqlAstJoinType.INNER || lhs.canUseInnerJoins() && !isNullable;
    TableGroup realParentTableGroup = lhs;
    while (realParentTableGroup.getModelPart() instanceof EmbeddableValuedModelPart) {
        realParentTableGroup = fromClauseAccess.findTableGroup(realParentTableGroup.getNavigablePath().getParent());
    }
    final TableGroupProducer tableGroupProducer;
    if (realParentTableGroup instanceof CorrelatedTableGroup) {
        // If the parent is a correlated table group, we can't refer to columns of the table in the outer query,
        // because the context in which a column is used could be an aggregate function.
        // Using a parent column in such a case would lead to an error if the parent query lacks a proper group by
        tableGroupProducer = entityMappingType;
    } else {
        tableGroupProducer = this;
    }
    final LazyTableGroup lazyTableGroup = new LazyTableGroup(canUseInnerJoin, navigablePath, fetched, () -> createTableGroupInternal(canUseInnerJoin, navigablePath, fetched, null, sqlAliasBase, sqlExpressionResolver, creationContext), (np, tableExpression) -> {
        if (!canUseParentTableGroup || tableGroupProducer != ToOneAttributeMapping.this) {
            return false;
        }
        NavigablePath path = np.getParent();
        // Fast path
        if (path != null && navigablePath.equals(path)) {
            return targetKeyPropertyNames.contains(np.getUnaliasedLocalName()) && identifyingColumnsTableExpression.equals(tableExpression);
        }
        final StringBuilder sb = new StringBuilder(np.getFullPath().length());
        sb.append(np.getUnaliasedLocalName());
        while (path != null && !navigablePath.equals(path)) {
            sb.insert(0, '.');
            sb.insert(0, path.getUnaliasedLocalName());
            path = path.getParent();
        }
        return path != null && navigablePath.equals(path) && targetKeyPropertyNames.contains(sb.toString()) && identifyingColumnsTableExpression.equals(tableExpression);
    }, tableGroupProducer, explicitSourceAlias, sqlAliasBase, creationContext.getSessionFactory(), lhs);
    if (predicateConsumer != null) {
        final TableReference lhsTableReference = lhs.resolveTableReference(navigablePath, identifyingColumnsTableExpression);
        lazyTableGroup.setTableGroupInitializerCallback(tableGroup -> predicateConsumer.accept(foreignKeyDescriptor.generateJoinPredicate(sideNature == ForeignKeyDescriptor.Nature.TARGET ? lhsTableReference : tableGroup.getPrimaryTableReference(), sideNature == ForeignKeyDescriptor.Nature.TARGET ? tableGroup.getPrimaryTableReference() : lhsTableReference, sqlExpressionResolver, creationContext)));
    }
    if (realParentTableGroup instanceof CorrelatedTableGroup) {
        // Force initialization of the underlying table group join to retain cardinality
        lazyTableGroup.getPrimaryTableReference();
    } else {
        initializeIfNeeded(lhs, requestedJoinType, lazyTableGroup);
    }
    return lazyTableGroup;
}
Also used : TableReference(org.hibernate.sql.ast.tree.from.TableReference) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) MappedByTableGroup(org.hibernate.sql.ast.tree.from.MappedByTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) StandardTableGroup(org.hibernate.sql.ast.tree.from.StandardTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) TreatedNavigablePath(org.hibernate.query.spi.TreatedNavigablePath) NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) TableGroupProducer(org.hibernate.sql.ast.tree.from.TableGroupProducer) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) SqlAliasBase(org.hibernate.sql.ast.spi.SqlAliasBase) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)

Aggregations

EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 TreatedNavigablePath (org.hibernate.query.spi.TreatedNavigablePath)1 EntityIdentifierNavigablePath (org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath)1 SqlAstJoinType (org.hibernate.sql.ast.SqlAstJoinType)1 SqlAliasBase (org.hibernate.sql.ast.spi.SqlAliasBase)1 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)1 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)1 MappedByTableGroup (org.hibernate.sql.ast.tree.from.MappedByTableGroup)1 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)1 StandardTableGroup (org.hibernate.sql.ast.tree.from.StandardTableGroup)1 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)1 TableGroupProducer (org.hibernate.sql.ast.tree.from.TableGroupProducer)1 TableReference (org.hibernate.sql.ast.tree.from.TableReference)1