use of org.hibernate.sql.ast.spi.SqlAliasBaseConstant in project hibernate-orm by hibernate.
the class DynamicFetchBuilderLegacy method buildFetch.
@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
final DomainResultCreationStateImpl creationState = ResultsHelper.impl(domainResultCreationState);
final TableGroup ownerTableGroup = creationState.getFromClauseAccess().findByAlias(ownerTableAlias);
final AttributeMapping attributeMapping = parent.getReferencedMappingContainer().findContainingEntityMapping().findDeclaredAttributeMapping(fetchableName);
final TableGroup tableGroup;
if (attributeMapping instanceof TableGroupJoinProducer) {
final SqlAliasBase sqlAliasBase = new SqlAliasBaseConstant(tableAlias);
final TableGroupJoin tableGroupJoin = ((TableGroupJoinProducer) attributeMapping).createTableGroupJoin(fetchPath, ownerTableGroup, tableAlias, SqlAstJoinType.INNER, true, false, s -> sqlAliasBase, creationState.getSqlExpressionResolver(), creationState.getFromClauseAccess(), creationState.getCreationContext());
ownerTableGroup.addTableGroupJoin(tableGroupJoin);
creationState.getFromClauseAccess().registerTableGroup(fetchPath, tableGroup = tableGroupJoin.getJoinedGroup());
} else {
tableGroup = ownerTableGroup;
}
if (columnNames != null) {
final ForeignKeyDescriptor keyDescriptor;
if (attributeMapping instanceof PluralAttributeMapping) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) attributeMapping;
keyDescriptor = pluralAttributeMapping.getKeyDescriptor();
} else {
// Not sure if this fetch builder can also be used with other attribute mappings
assert attributeMapping instanceof ToOneAttributeMapping;
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
keyDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
}
if (!columnNames.isEmpty()) {
keyDescriptor.forEachSelectable((selectionIndex, selectableMapping) -> {
resolveSqlSelection(columnNames.get(selectionIndex), createColumnReferenceKey(tableGroup.resolveTableReference(selectableMapping.getContainingTableExpression()), selectableMapping.getSelectionExpression()), selectableMapping.getJdbcMapping(), jdbcResultsMetadata, domainResultCreationState);
});
}
// We process the fetch builder such that it contains a resultBuilderEntity before calling this method in ResultSetMappingProcessor
if (resultBuilderEntity != null) {
return resultBuilderEntity.buildFetch(parent, attributeMapping, jdbcResultsMetadata, creationState);
}
}
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)) {
return findFetchBuilder(relativePath.substring(prefix.length()));
}
return null;
});
return parent.generateFetchableFetch(attributeMapping, parent.resolveNavigablePath(attributeMapping), FetchTiming.IMMEDIATE, true, null, domainResultCreationState);
} finally {
creationState.popExplicitFetchMementoResolver();
}
}
use of org.hibernate.sql.ast.spi.SqlAliasBaseConstant 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();
}
}
Aggregations