Search in sources :

Example 1 with FetchBuilder

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;
}
Also used : FetchBuilder(org.hibernate.query.results.FetchBuilder)

Example 2 with 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);
}
Also used : DynamicFetchBuilder(org.hibernate.query.results.dynamic.DynamicFetchBuilder) FetchBuilder(org.hibernate.query.results.FetchBuilder) CompleteFetchBuilderEntityValuedModelPart(org.hibernate.query.results.complete.CompleteFetchBuilderEntityValuedModelPart) HashMap(java.util.HashMap) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) DynamicFetchBuilder(org.hibernate.query.results.dynamic.DynamicFetchBuilder) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) DynamicResultBuilderEntityStandard(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with FetchBuilder

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);
}
Also used : BasicValuedFetchBuilder(org.hibernate.query.results.BasicValuedFetchBuilder) ImplicitFetchBuilderBasic(org.hibernate.query.results.implicit.ImplicitFetchBuilderBasic) FetchBuilder(org.hibernate.query.results.FetchBuilder) BasicValuedFetchBuilder(org.hibernate.query.results.BasicValuedFetchBuilder) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) CompleteResultBuilderEntityJpa(org.hibernate.query.results.complete.CompleteResultBuilderEntityJpa) HashMap(java.util.HashMap) DelayedFetchBuilderBasicPart(org.hibernate.query.results.complete.DelayedFetchBuilderBasicPart) EntityDiscriminatorMapping(org.hibernate.metamodel.mapping.EntityDiscriminatorMapping)

Example 4 with FetchBuilder

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);
}
Also used : BasicValuedFetchBuilder(org.hibernate.query.results.BasicValuedFetchBuilder) FetchBuilder(org.hibernate.query.results.FetchBuilder) BasicValuedFetchBuilder(org.hibernate.query.results.BasicValuedFetchBuilder) HashMap(java.util.HashMap) CompleteResultBuilderEntityStandard(org.hibernate.query.results.complete.CompleteResultBuilderEntityStandard)

Example 5 with FetchBuilder

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();
    }
}
Also used : DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FetchBuilder(org.hibernate.query.results.FetchBuilder) NavigablePath(org.hibernate.query.spi.NavigablePath) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) TableReference(org.hibernate.sql.ast.tree.from.TableReference) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) TableGroupImpl(org.hibernate.query.results.TableGroupImpl)

Aggregations

FetchBuilder (org.hibernate.query.results.FetchBuilder)5 HashMap (java.util.HashMap)3 BasicValuedFetchBuilder (org.hibernate.query.results.BasicValuedFetchBuilder)2 Map (java.util.Map)1 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)1 EntityDiscriminatorMapping (org.hibernate.metamodel.mapping.EntityDiscriminatorMapping)1 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)1 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)1 DomainResultCreationStateImpl (org.hibernate.query.results.DomainResultCreationStateImpl)1 TableGroupImpl (org.hibernate.query.results.TableGroupImpl)1 CompleteFetchBuilderEntityValuedModelPart (org.hibernate.query.results.complete.CompleteFetchBuilderEntityValuedModelPart)1 CompleteResultBuilderEntityJpa (org.hibernate.query.results.complete.CompleteResultBuilderEntityJpa)1 CompleteResultBuilderEntityStandard (org.hibernate.query.results.complete.CompleteResultBuilderEntityStandard)1 DelayedFetchBuilderBasicPart (org.hibernate.query.results.complete.DelayedFetchBuilderBasicPart)1 DynamicFetchBuilder (org.hibernate.query.results.dynamic.DynamicFetchBuilder)1 DynamicFetchBuilderLegacy (org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy)1 DynamicResultBuilderEntityStandard (org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard)1 ImplicitFetchBuilderBasic (org.hibernate.query.results.implicit.ImplicitFetchBuilderBasic)1