Search in sources :

Example 1 with ToOneAttributeMapping

use of org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping in project hibernate-orm by hibernate.

the class AbstractCompositeIdentifierMapping method forEachJdbcValue.

@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
    int span = 0;
    final List<AttributeMapping> attributeMappings = getEmbeddableTypeDescriptor().getAttributeMappings();
    for (int i = 0; i < attributeMappings.size(); i++) {
        final AttributeMapping attributeMapping = attributeMappings.get(i);
        final Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
        if (attributeMapping instanceof ToOneAttributeMapping) {
            final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
            final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
            final Object identifier = fkDescriptor.getAssociationKeyFromSide(o, toOneAttributeMapping.getSideNature().inverse(), session);
            span += fkDescriptor.forEachJdbcValue(identifier, clause, span + offset, valuesConsumer, session);
        } else {
            span += attributeMapping.forEachJdbcValue(o, clause, span + offset, valuesConsumer, session);
        }
    }
    return span;
}
Also used : ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping)

Example 2 with ToOneAttributeMapping

use of org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping in project hibernate-orm by hibernate.

the class DynamicFetchBuilderStandard method buildFetch.

@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
    final TableGroup ownerTableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(parent.getNavigablePath());
    final Fetchable attributeMapping = (Fetchable) parent.getReferencedMappingContainer().findSubPart(fetchableName, null);
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    final SelectableConsumer selectableConsumer = (selectionIndex, selectableMapping) -> {
        final TableReference tableReference = ownerTableGroup.resolveTableReference(fetchPath, selectableMapping.getContainingTableExpression());
        final String columnAlias = columnNames.get(selectionIndex);
        sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selectableMapping.getSelectionExpression()), state -> {
            final int resultSetPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
            final int valuesArrayPosition = resultSetPosition - 1;
            return new ResultSetMappingSqlSelection(valuesArrayPosition, selectableMapping.getJdbcMapping());
        }), selectableMapping.getJdbcMapping().getMappedJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
    };
    if (attributeMapping instanceof BasicValuedMapping) {
        attributeMapping.forEachSelectable(selectableConsumer);
        return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.IMMEDIATE, true, null, creationStateImpl);
    } else if (attributeMapping instanceof EmbeddedAttributeMapping) {
        attributeMapping.forEachSelectable(selectableConsumer);
        return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.IMMEDIATE, false, null, creationStateImpl);
    } else if (attributeMapping instanceof ToOneAttributeMapping) {
        final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
        toOneAttributeMapping.getForeignKeyDescriptor().getPart(toOneAttributeMapping.getSideNature()).forEachSelectable(selectableConsumer);
        return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.DELAYED, false, null, creationStateImpl);
    } else {
        assert attributeMapping instanceof PluralAttributeMapping;
        final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) attributeMapping;
        pluralAttributeMapping.getKeyDescriptor().visitTargetSelectables(selectableConsumer);
        return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.DELAYED, false, null, creationStateImpl);
    }
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) Fetchable(org.hibernate.sql.results.graph.Fetchable) FetchTiming(org.hibernate.engine.FetchTiming) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) Fetch(org.hibernate.sql.results.graph.Fetch) ArrayList(java.util.ArrayList) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) TableReference(org.hibernate.sql.ast.tree.from.TableReference) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ResultsHelper(org.hibernate.query.results.ResultsHelper) List(java.util.List) NativeQuery(org.hibernate.query.NativeQuery) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) Fetchable(org.hibernate.sql.results.graph.Fetchable) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) TableReference(org.hibernate.sql.ast.tree.from.TableReference) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver)

Example 3 with ToOneAttributeMapping

use of org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping in project hibernate-orm by hibernate.

the class CircularFetchImpl method createAssembler.

@Override
public DomainResultAssembler createAssembler(FetchParentAccess parentAccess, AssemblerCreationState creationState) {
    final DomainResultAssembler resultAssembler = keyResult.createResultAssembler(parentAccess, creationState);
    final EntityInitializer initializer = (EntityInitializer) creationState.resolveInitializer(getNavigablePath(), referencedModelPart, () -> {
        if (timing == FetchTiming.IMMEDIATE) {
            if (selectByUniqueKey) {
                return new EntitySelectFetchByUniqueKeyInitializer(parentAccess, fetchable, getNavigablePath(), entityMappingType.getEntityPersister(), resultAssembler);
            }
            final EntityPersister entityPersister = entityMappingType.getEntityPersister();
            if (entityPersister.isBatchLoadable()) {
                return new BatchEntitySelectFetchInitializer(parentAccess, (ToOneAttributeMapping) referencedModelPart, getReferencedPath(), entityPersister, resultAssembler);
            } else {
                return new EntitySelectFetchInitializer(parentAccess, (ToOneAttributeMapping) referencedModelPart, getReferencedPath(), entityPersister, resultAssembler);
            }
        } else {
            return new EntityDelayedFetchInitializer(parentAccess, getReferencedPath(), fetchable, selectByUniqueKey, resultAssembler);
        }
    });
    return new BiDirectionalFetchAssembler(initializer, fetchable.getJavaType());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) DomainResultAssembler(org.hibernate.sql.results.graph.DomainResultAssembler) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) EntityDelayedFetchInitializer(org.hibernate.sql.results.graph.entity.internal.EntityDelayedFetchInitializer) BatchEntitySelectFetchInitializer(org.hibernate.sql.results.graph.entity.internal.BatchEntitySelectFetchInitializer) EntitySelectFetchInitializer(org.hibernate.sql.results.graph.entity.internal.EntitySelectFetchInitializer) EntityInitializer(org.hibernate.sql.results.graph.entity.EntityInitializer) EntitySelectFetchByUniqueKeyInitializer(org.hibernate.sql.results.graph.entity.internal.EntitySelectFetchByUniqueKeyInitializer) BatchEntitySelectFetchInitializer(org.hibernate.sql.results.graph.entity.internal.BatchEntitySelectFetchInitializer)

Example 4 with ToOneAttributeMapping

use of org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping 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 5 with ToOneAttributeMapping

use of org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping in project hibernate-orm by hibernate.

the class PluralAttributePath method resolvePathPart.

@Override
public DomainPath resolvePathPart(String name, String identifier, boolean isTerminal, TranslationContext translationContext) {
    final ModelPart subPart = pluralAttributeMapping.findSubPart(name, null);
    if (subPart != null) {
        if (subPart instanceof CollectionPart) {
            return new CollectionPartPath(this, (CollectionPart) subPart);
        }
        if (subPart instanceof EmbeddableValuedModelPart) {
            return new DomainPathContinuation(navigablePath.append(name), this, subPart);
        }
        if (subPart instanceof ToOneAttributeMapping) {
            return new FkDomainPathContinuation(navigablePath.append(name), this, (ToOneAttributeMapping) subPart);
        }
        // leaf case:
        final CollectionPartPath elementPath = new CollectionPartPath(this, pluralAttributeMapping.getElementDescriptor());
        return (DomainPath) elementPath.resolvePathPart(name, identifier, isTerminal, translationContext);
    }
    if (pluralAttributeMapping.getElementDescriptor() instanceof EmbeddableValuedModelPart) {
        final EmbeddableValuedModelPart elementDescriptor = (EmbeddableValuedModelPart) pluralAttributeMapping.getElementDescriptor();
        final ModelPart elementSubPart = elementDescriptor.findSubPart(name, null);
        if (elementSubPart != null) {
            // create the CollectionSubPath to use as the `lhs` for the element sub-path
            final CollectionPartPath elementPath = new CollectionPartPath(this, (CollectionPart) elementDescriptor);
            return new DomainPathContinuation(elementPath.getNavigablePath().append(name), this, elementSubPart);
        }
    }
    if (pluralAttributeMapping.getIndexDescriptor() instanceof EmbeddableValuedModelPart) {
        final EmbeddableValuedModelPart indexDescriptor = (EmbeddableValuedModelPart) pluralAttributeMapping.getIndexDescriptor();
        final ModelPart indexSubPart = indexDescriptor.findSubPart(name, null);
        if (indexSubPart != null) {
            // create the CollectionSubPath to use as the `lhs` for the element sub-path
            final CollectionPartPath indexPath = new CollectionPartPath(this, (CollectionPart) indexDescriptor);
            return new DomainPathContinuation(indexPath.getNavigablePath().append(name), this, indexSubPart);
        }
    }
    return null;
}
Also used : AbstractDomainPath(org.hibernate.metamodel.mapping.internal.AbstractDomainPath) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart)

Aggregations

ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)18 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)10 ModelPart (org.hibernate.metamodel.mapping.ModelPart)10 EntityPersister (org.hibernate.persister.entity.EntityPersister)9 Test (org.junit.jupiter.api.Test)8 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)4 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)4 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)4 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)3 NavigablePath (org.hibernate.query.spi.NavigablePath)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 FetchTiming (org.hibernate.engine.FetchTiming)2 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)2 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)2 CollectionPart (org.hibernate.metamodel.mapping.CollectionPart)2 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)2 EntityAssociationMapping (org.hibernate.metamodel.mapping.EntityAssociationMapping)2