use of org.hibernate.query.results.FetchBuilder in project hibernate-orm by hibernate.
the class AbstractFetchBuilderContainer method addProperty.
@Override
public DynamicFetchBuilder addProperty(String propertyName) {
if (fetchBuilderMap == null) {
fetchBuilderMap = new HashMap<>();
} else {
final FetchBuilder existing = fetchBuilderMap.get(propertyName);
if (existing != null) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "Fetch was already defined for %s.%s : %s", getPropertyBase(), propertyName, existing));
}
}
final DynamicFetchBuilderStandard fetchBuilder = new DynamicFetchBuilderStandard(propertyName);
fetchBuilderMap.put(propertyName, fetchBuilder);
return fetchBuilder;
}
use of org.hibernate.query.results.FetchBuilder in project hibernate-orm by hibernate.
the class FetchMementoHbmStandard method resolve.
@Override
public FetchBuilder resolve(Parent parent, Consumer<String> querySpaceConsumer, ResultSetMappingResolutionContext context) {
final Map<String, FetchBuilder> fetchBuilderMap = new HashMap<>();
fetchMementoMap.forEach((attrName, fetchMemento) -> fetchBuilderMap.put(attrName, fetchMemento.resolve(this, querySpaceConsumer, context)));
final DynamicResultBuilderEntityStandard resultBuilder;
if (fetchable instanceof PluralAttributeMapping) {
resultBuilder = new DynamicResultBuilderEntityStandard((EntityMappingType) ((PluralAttributeMapping) fetchable).getElementDescriptor().getPartMappingType(), tableAlias, navigablePath);
FetchBuilder element = fetchBuilderMap.get("element");
if (element != null) {
if (element instanceof DynamicFetchBuilder) {
resultBuilder.addIdColumnAliases(((DynamicFetchBuilder) element).getColumnAliases().toArray(new String[0]));
} else {
resultBuilder.addIdColumnAliases(((CompleteFetchBuilderEntityValuedModelPart) element).getColumnAliases().toArray(new String[0]));
}
}
FetchBuilder index = fetchBuilderMap.get("index");
if (index != null) {
resultBuilder.addFetchBuilder(CollectionPart.Nature.INDEX.getName(), index);
}
for (Map.Entry<String, FetchBuilder> entry : fetchBuilderMap.entrySet()) {
if (entry.getKey().startsWith(ELEMENT_PREFIX)) {
resultBuilder.addFetchBuilder(entry.getKey().substring(ELEMENT_PREFIX.length()), entry.getValue());
}
}
} else {
resultBuilder = new DynamicResultBuilderEntityStandard(((ToOneAttributeMapping) fetchable).getEntityMappingType(), tableAlias, navigablePath);
fetchBuilderMap.forEach(resultBuilder::addFetchBuilder);
}
return new DynamicFetchBuilderLegacy(tableAlias, ownerTableAlias, fetchable.getFetchableName(), keyColumnNames, fetchBuilderMap, resultBuilder);
}
use of org.hibernate.query.results.FetchBuilder in project hibernate-orm by hibernate.
the class ResultMementoEntityJpa method resolve.
@Override
public ResultBuilderEntityValued resolve(Consumer<String> querySpaceConsumer, ResultSetMappingResolutionContext context) {
final EntityDiscriminatorMapping discriminatorMapping = entityDescriptor.getDiscriminatorMapping();
final BasicValuedFetchBuilder discriminatorFetchBuilder;
if (discriminatorMapping == null) {
assert discriminatorMemento == null;
discriminatorFetchBuilder = null;
} else {
if (discriminatorMemento != null) {
discriminatorFetchBuilder = (BasicValuedFetchBuilder) discriminatorMemento.resolve(this, querySpaceConsumer, context);
} else {
discriminatorFetchBuilder = new ImplicitFetchBuilderBasic(navigablePath, discriminatorMapping);
}
}
final HashMap<String, FetchBuilder> explicitFetchBuilderMap = new HashMap<>();
// If there are no explicit fetches, we don't register DELAYED builders to get implicit fetching of all basic fetchables
if (!explicitFetchMementoMap.isEmpty()) {
explicitFetchMementoMap.forEach((relativePath, fetchMemento) -> explicitFetchBuilderMap.put(relativePath, fetchMemento.resolve(this, querySpaceConsumer, context)));
final boolean isEnhancedForLazyLoading = entityDescriptor.getRepresentationStrategy().isBytecodeEnhanced();
// Implicit basic fetches are DELAYED by default, so register fetch builders for the remaining basic fetchables
entityDescriptor.visitAttributeMappings(attributeMapping -> {
final Function<String, FetchBuilder> fetchBuilderCreator;
if (attributeMapping instanceof BasicValuedModelPart) {
fetchBuilderCreator = k -> new DelayedFetchBuilderBasicPart(navigablePath.append(k), (BasicValuedModelPart) attributeMapping, isEnhancedForLazyLoading);
explicitFetchBuilderMap.computeIfAbsent(attributeMapping.getFetchableName(), fetchBuilderCreator);
}
});
}
return new CompleteResultBuilderEntityJpa(navigablePath, entityDescriptor, lockMode, discriminatorFetchBuilder, explicitFetchBuilderMap);
}
use of org.hibernate.query.results.FetchBuilder in project hibernate-orm by hibernate.
the class ResultMementoEntityStandard method resolve.
@Override
public ResultBuilderEntityValued resolve(Consumer<String> querySpaceConsumer, ResultSetMappingResolutionContext context) {
final BasicValuedFetchBuilder discriminatorResultBuilder = discriminatorMemento != null ? (BasicValuedFetchBuilder) discriminatorMemento.resolve(this, querySpaceConsumer, context) : null;
final HashMap<String, FetchBuilder> fetchBuilderMap = new HashMap<>();
fetchMementoMap.forEach((attrName, fetchMemento) -> fetchBuilderMap.put(attrName, fetchMemento.resolve(this, querySpaceConsumer, context)));
return new CompleteResultBuilderEntityStandard(tableAlias, navigablePath, entityDescriptor, lockMode, discriminatorResultBuilder, fetchBuilderMap);
}
use of org.hibernate.query.results.FetchBuilder 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