use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.
the class DynamicResultBuilderEntityStandard method buildResultOrFetch.
private <T> T buildResultOrFetch(Function<TableGroup, T> resultOrFetchBuilder, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationState domainResultCreationState) {
final DomainResultCreationStateImpl creationState = impl(domainResultCreationState);
final FromClauseAccess fromClauseAccess = domainResultCreationState.getSqlAstCreationState().getFromClauseAccess();
final TableGroup tableGroup = fromClauseAccess.resolveTableGroup(navigablePath, np -> {
final TableReference tableReference = entityMapping.createPrimaryTableReference(new SqlAliasBaseConstant(tableAlias), creationState.getSqlExpressionResolver(), creationState.getCreationContext());
if (lockMode != null) {
domainResultCreationState.getSqlAstCreationState().registerLockMode(tableAlias, lockMode);
}
return new TableGroupImpl(navigablePath, tableAlias, tableReference, entityMapping);
});
final TableReference tableReference = tableGroup.getPrimaryTableReference();
final List<String> idColumnAliases;
final FetchBuilder idFetchBuilder;
if (this.idColumnNames != null) {
idColumnAliases = this.idColumnNames;
} else if ((idFetchBuilder = findIdFetchBuilder()) != null) {
idColumnAliases = ((DynamicFetchBuilder) idFetchBuilder).getColumnAliases();
} else {
idColumnAliases = null;
}
if (idColumnAliases != null) {
final EntityIdentifierMapping identifierMapping = entityMapping.getIdentifierMapping();
identifierMapping.forEachSelectable((selectionIndex, selectableMapping) -> {
resolveSqlSelection(idColumnAliases.get(selectionIndex), createColumnReferenceKey(tableReference, selectableMapping.getSelectionExpression()), selectableMapping.getJdbcMapping(), jdbcResultsMetadata, domainResultCreationState);
});
}
if (discriminatorColumnName != null) {
resolveSqlSelection(discriminatorColumnName, createColumnReferenceKey(tableReference, entityMapping.getDiscriminatorMapping().getSelectionExpression()), entityMapping.getDiscriminatorMapping().getJdbcMapping(), jdbcResultsMetadata, domainResultCreationState);
}
try {
final NavigablePath currentRelativePath = creationState.getCurrentRelativePath();
final String prefix;
if (currentRelativePath == null) {
prefix = "";
} else {
prefix = currentRelativePath.getFullPath().replace(ELEMENT_PREFIX, "").replace(INDEX_PREFIX, "") + ".";
}
creationState.pushExplicitFetchMementoResolver(relativePath -> {
if (relativePath.startsWith(prefix)) {
final int startIndex;
if (relativePath.regionMatches(prefix.length(), ELEMENT_PREFIX, 0, ELEMENT_PREFIX.length())) {
startIndex = prefix.length() + ELEMENT_PREFIX.length();
} else {
startIndex = prefix.length();
}
return findFetchBuilder(relativePath.substring(startIndex));
}
return null;
});
return resultOrFetchBuilder.apply(tableGroup);
} finally {
creationState.popExplicitFetchMementoResolver();
}
}
use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.
the class PluralAttributeMappingImpl method resolveCollectionTableGroup.
private TableGroup resolveCollectionTableGroup(FetchParent fetchParent, NavigablePath fetchablePath, DomainResultCreationState creationState, SqlAstCreationState sqlAstCreationState) {
final FromClauseAccess fromClauseAccess = sqlAstCreationState.getFromClauseAccess();
return fromClauseAccess.resolveTableGroup(fetchablePath, p -> {
final TableGroup lhsTableGroup = fromClauseAccess.getTableGroup(fetchParent.getNavigablePath());
final TableGroupJoin tableGroupJoin = createTableGroupJoin(fetchablePath, lhsTableGroup, null, SqlAstJoinType.LEFT, true, false, creationState.getSqlAstCreationState());
lhsTableGroup.addTableGroupJoin(tableGroupJoin);
return tableGroupJoin.getJoinedGroup();
});
}
use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.
the class AnyDiscriminatorPart method generateFetch.
@Override
public Fetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
final SessionFactoryImplementor sessionFactory = sqlAstCreationState.getCreationContext().getSessionFactory();
final FromClauseAccess fromClauseAccess = sqlAstCreationState.getFromClauseAccess();
final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
final TableGroup tableGroup = fromClauseAccess.getTableGroup(fetchablePath.getParent().getParent());
final TableReference tableReference = tableGroup.resolveTableReference(fetchablePath, table);
final Expression columnReference = sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, column), processingState -> new ColumnReference(tableReference, column, false, null, null, jdbcMapping(), sessionFactory));
final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(columnReference, jdbcMapping().getMappedJavaType(), fetchParent, sessionFactory.getTypeConfiguration());
return new BasicFetch<>(sqlSelection.getValuesArrayPosition(), fetchParent, fetchablePath, this, null, fetchTiming, creationState);
}
use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.
the class AnyKeyPart method generateFetch.
@Override
public Fetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
final FromClauseAccess fromClauseAccess = creationState.getSqlAstCreationState().getFromClauseAccess();
final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlAstCreationState().getSqlExpressionResolver();
final SessionFactoryImplementor sessionFactory = creationState.getSqlAstCreationState().getCreationContext().getSessionFactory();
final TableGroup tableGroup = fromClauseAccess.getTableGroup(fetchParent.getNavigablePath().getParent());
final TableReference tableReference = tableGroup.resolveTableReference(fetchablePath, table);
final Expression columnReference = sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, column), processingState -> new ColumnReference(tableReference, column, false, null, null, jdbcMapping, sessionFactory));
final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(columnReference, getJavaType(), fetchParent, sessionFactory.getTypeConfiguration());
return new BasicFetch<>(sqlSelection.getValuesArrayPosition(), fetchParent, fetchablePath, this, null, fetchTiming, creationState);
}
use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.
the class ToOneAttributeMapping method determineTableGroup.
private TableGroup determineTableGroup(NavigablePath fetchablePath, FetchParent fetchParent, TableGroup parentTableGroup, String resultVariable, FromClauseAccess fromClauseAccess, DomainResultCreationState creationState) {
final TableGroup tableGroup;
if (fetchParent instanceof EntityResultJoinedSubclassImpl && ((EntityPersister) fetchParent.getReferencedModePart()).findDeclaredAttributeMapping(getPartName()) == null) {
final TableGroupJoin tableGroupJoin = createTableGroupJoin(fetchablePath, parentTableGroup, resultVariable, getJoinType(fetchablePath, parentTableGroup), true, false, creationState.getSqlAstCreationState());
parentTableGroup.addTableGroupJoin(tableGroupJoin);
tableGroup = tableGroupJoin.getJoinedGroup();
fromClauseAccess.registerTableGroup(fetchablePath, tableGroup);
} else {
tableGroup = fromClauseAccess.resolveTableGroup(fetchablePath, np -> {
final TableGroupJoin tableGroupJoin = createTableGroupJoin(fetchablePath, parentTableGroup, resultVariable, getDefaultSqlAstJoinType(parentTableGroup), true, false, creationState.getSqlAstCreationState());
parentTableGroup.addTableGroupJoin(tableGroupJoin);
return tableGroupJoin.getJoinedGroup();
});
}
return tableGroup;
}
Aggregations