Search in sources :

Example 1 with NonAggregatedIdentifierMapping

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

the class DefaultLoadEventListener method checkIdClass.

private void checkIdClass(final EntityPersister persister, final LoadEvent event, final LoadType loadType, final Class<?> idClass) {
    // we may have the jpa requirement of allowing find-by-id where id is the "simple pk value" of a
    // dependent objects parent.  This is part of its generally goofy derived identity "feature"
    final EntityIdentifierMapping idMapping = persister.getIdentifierMapping();
    if (idMapping instanceof CompositeIdentifierMapping) {
        final CompositeIdentifierMapping compositeIdMapping = (CompositeIdentifierMapping) idMapping;
        final List<AttributeMapping> attributeMappings = compositeIdMapping.getPartMappingType().getAttributeMappings();
        if (attributeMappings.size() == 1) {
            final AttributeMapping singleIdAttribute = attributeMappings.get(0);
            if (singleIdAttribute.getMappedType() instanceof EntityMappingType) {
                final EntityMappingType parentIdTargetMapping = (EntityMappingType) singleIdAttribute.getMappedType();
                final EntityIdentifierMapping parentIdTargetIdMapping = parentIdTargetMapping.getIdentifierMapping();
                final MappingType parentIdType = parentIdTargetIdMapping instanceof CompositeIdentifierMapping ? ((CompositeIdentifierMapping) parentIdTargetIdMapping).getMappedIdEmbeddableTypeDescriptor() : parentIdTargetIdMapping.getMappedType();
                if (parentIdType.getMappedJavaType().getJavaTypeClass().isInstance(event.getEntityId())) {
                    // yep that's what we have...
                    loadByDerivedIdentitySimplePkValue(event, loadType, persister, compositeIdMapping, (EntityPersister) parentIdTargetMapping);
                    return;
                }
            }
        } else if (idMapping instanceof NonAggregatedIdentifierMapping) {
            if (idClass.isInstance(event.getEntityId())) {
                return;
            }
        }
    }
    throw new TypeMismatchException("Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass());
}
Also used : EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) MappingType(org.hibernate.metamodel.mapping.MappingType) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) TypeMismatchException(org.hibernate.TypeMismatchException) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 2 with NonAggregatedIdentifierMapping

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

the class AbstractEntityPersister method getPropertyValue.

@Override
public Object getPropertyValue(Object object, String propertyName) {
    final int dotIndex = propertyName.indexOf('.');
    final String basePropertyName = dotIndex == -1 ? propertyName : propertyName.substring(0, dotIndex);
    final AttributeMapping attributeMapping = findAttributeMapping(basePropertyName);
    ManagedMappingType baseValueType = null;
    Object baseValue = null;
    if (attributeMapping != null) {
        baseValue = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
        if (dotIndex != -1) {
            baseValueType = (ManagedMappingType) attributeMapping.getMappedType();
        }
    } else if (identifierMapping instanceof NonAggregatedIdentifierMapping) {
        final EmbeddedAttributeMapping embeddedAttributeMapping = (EmbeddedAttributeMapping) findAttributeMapping(NavigableRole.IDENTIFIER_MAPPER_PROPERTY);
        final AttributeMapping mapping = embeddedAttributeMapping.getMappedType().findAttributeMapping(basePropertyName);
        if (mapping != null) {
            baseValue = mapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
            if (dotIndex != -1) {
                baseValueType = (ManagedMappingType) mapping.getMappedType();
            }
        }
    }
    return getPropertyValue(baseValue, baseValueType, propertyName, dotIndex);
}
Also used : ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) DiscriminatedAssociationAttributeMapping(org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping)

Example 3 with NonAggregatedIdentifierMapping

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

the class NonAggregatedCompositeValuedPathInterpretation method from.

public static <T> NonAggregatedCompositeValuedPathInterpretation<T> from(NonAggregatedCompositeSimplePath<T> sqmPath, SqmToSqlAstConverter converter, SqmToSqlAstConverter sqlAstCreationState) {
    final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().findTableGroup(sqmPath.getLhs().getNavigablePath());
    final NonAggregatedIdentifierMapping mapping = (NonAggregatedIdentifierMapping) tableGroup.getModelPart().findSubPart(sqmPath.getReferencedPathSource().getPathName(), null);
    return new NonAggregatedCompositeValuedPathInterpretation<>(mapping.toSqlExpression(tableGroup, converter.getCurrentClauseStack().getCurrent(), converter, converter), sqmPath.getNavigablePath(), mapping, tableGroup);
}
Also used : NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup)

Example 4 with NonAggregatedIdentifierMapping

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

the class DomainResultCreationStateImpl method visitFetches.

@Override
public List<Fetch> visitFetches(FetchParent fetchParent) {
    final FetchableContainer fetchableContainer = fetchParent.getReferencedMappingContainer();
    final List<Fetch> fetches = CollectionHelper.arrayList(fetchableContainer.getNumberOfFetchables());
    final Consumer<Fetchable> fetchableConsumer = fetchable -> {
        final String fetchableName = fetchable.getFetchableName();
        Map.Entry<String, NavigablePath> currentEntry;
        if (relativePathStack.isEmpty()) {
            currentEntry = new AbstractMap.SimpleEntry<>(getRelativePath("", fetchable, fetchableContainer), new NavigablePath(fetchableName));
        } else {
            final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
            final String key = oldEntry.getKey();
            currentEntry = new AbstractMap.SimpleEntry<>(getRelativePath(key, fetchable, fetchableContainer), oldEntry.getValue().append(fetchableName));
        }
        // todo (6.0): figure out if we can somehow create the navigable paths in a better way
        final String fullPath = currentEntry.getKey();
        FetchBuilder explicitFetchBuilder = fetchBuilderResolverStack.getCurrent().apply(fullPath);
        DynamicFetchBuilderLegacy fetchBuilderLegacy;
        if (explicitFetchBuilder == null) {
            fetchBuilderLegacy = legacyFetchResolver.resolve(fromClauseAccess.findTableGroup(fetchParent.getNavigablePath()).getPrimaryTableReference().getIdentificationVariable(), fetchableName);
        } else {
            fetchBuilderLegacy = null;
        }
        if (fetchable instanceof Association && fetchable.getMappedFetchOptions().getTiming() == FetchTiming.DELAYED) {
            final Association association = (Association) fetchable;
            final ForeignKeyDescriptor foreignKeyDescriptor = association.getForeignKeyDescriptor();
            final String partName = attributeName(foreignKeyDescriptor.getSide(association.getSideNature().inverse()).getModelPart());
            // If there are no fetch builders for this association, we only want to fetch the FK
            if (explicitFetchBuilder == null && fetchBuilderLegacy == null && partName != null) {
                currentEntry = new AbstractMap.SimpleEntry<>(currentEntry.getKey() + "." + partName, currentEntry.getValue().append(partName));
                explicitFetchBuilder = fetchBuilderResolverStack.getCurrent().apply(currentEntry.getKey());
                if (explicitFetchBuilder == null) {
                    fetchBuilderLegacy = legacyFetchResolver.resolve(fromClauseAccess.findTableGroup(fetchParent.getNavigablePath()).getPrimaryTableReference().getIdentificationVariable(), fetchableName);
                }
            }
        }
        relativePathStack.push(currentEntry);
        try {
            final NavigablePath fetchPath = fetchParent.resolveNavigablePath(fetchable);
            final FetchBuilder fetchBuilder;
            if (explicitFetchBuilder != null) {
                fetchBuilder = explicitFetchBuilder;
            } else {
                if (fetchBuilderLegacy == null) {
                    fetchBuilder = Builders.implicitFetchBuilder(fetchPath, fetchable, this);
                } else {
                    fetchBuilder = fetchBuilderLegacy;
                }
            }
            final Fetch fetch = fetchBuilder.buildFetch(fetchParent, fetchPath, jdbcResultsMetadata, (s, s2) -> {
                throw new UnsupportedOperationException();
            }, this);
            fetches.add(fetch);
        } finally {
            relativePathStack.pop();
        }
    };
    boolean previous = this.processingKeyFetches;
    this.processingKeyFetches = true;
    if (fetchableContainer instanceof EntityValuedModelPart) {
        final EntityValuedModelPart entityValuedFetchable = (EntityValuedModelPart) fetchableContainer;
        final EntityIdentifierMapping identifierMapping = entityValuedFetchable.getEntityMappingType().getIdentifierMapping();
        final boolean idClass = identifierMapping instanceof NonAggregatedIdentifierMapping;
        final String identifierAttributeName = attributeName(identifierMapping);
        if (idClass) {
            final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
            relativePathStack.push(new AbstractMap.SimpleEntry<>(oldEntry == null ? "" : oldEntry.getKey(), new EntityIdentifierNavigablePath(oldEntry == null ? fetchParent.getNavigablePath() : oldEntry.getValue(), identifierAttributeName)));
        } else if (identifierMapping instanceof CompositeIdentifierMapping) {
            final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
            relativePathStack.push(new AbstractMap.SimpleEntry<>(oldEntry == null ? identifierAttributeName : oldEntry.getKey() + "." + identifierAttributeName, new EntityIdentifierNavigablePath(oldEntry == null ? fetchParent.getNavigablePath() : oldEntry.getValue(), identifierAttributeName)));
        }
        try {
            if (identifierMapping instanceof FetchableContainer) {
                // essentially means the entity has a composite id - ask the embeddable to visit its fetchables
                ((FetchableContainer) identifierMapping).visitFetchables(fetchableConsumer, null);
            } else {
                fetchableConsumer.accept((Fetchable) identifierMapping);
            }
        } finally {
            this.processingKeyFetches = previous;
            if (idClass) {
                this.relativePathStack.pop();
            }
        }
    }
    fetchableContainer.visitKeyFetchables(fetchableConsumer, null);
    fetchableContainer.visitFetchables(fetchableConsumer, null);
    return fetches;
}
Also used : CollectionHelper(org.hibernate.internal.util.collections.CollectionHelper) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SqlAliasBaseGenerator(org.hibernate.sql.ast.spi.SqlAliasBaseGenerator) ResultsLogger(org.hibernate.sql.results.ResultsLogger) SingleAttributeIdentifierMapping(org.hibernate.metamodel.mapping.internal.SingleAttributeIdentifierMapping) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JavaType(org.hibernate.type.descriptor.java.JavaType) ResultsHelper.attributeName(org.hibernate.query.results.ResultsHelper.attributeName) HashMap(java.util.HashMap) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) Function(java.util.function.Function) BasicValuedCollectionPart(org.hibernate.metamodel.mapping.internal.BasicValuedCollectionPart) Internal(org.hibernate.Internal) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) ModelPart(org.hibernate.metamodel.mapping.ModelPart) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Map(java.util.Map) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) Fetchable(org.hibernate.sql.results.graph.Fetchable) FetchTiming(org.hibernate.engine.FetchTiming) LockMode(org.hibernate.LockMode) AssociationKey(org.hibernate.metamodel.mapping.AssociationKey) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) LegacyFetchResolver(org.hibernate.query.results.dynamic.LegacyFetchResolver) NavigablePath(org.hibernate.query.spi.NavigablePath) Expression(org.hibernate.sql.ast.tree.expression.Expression) Fetch(org.hibernate.sql.results.graph.Fetch) SqlAstProcessingState(org.hibernate.sql.ast.spi.SqlAstProcessingState) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Consumer(java.util.function.Consumer) StandardStack(org.hibernate.internal.util.collections.StandardStack) Association(org.hibernate.metamodel.mapping.Association) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) AbstractMap(java.util.AbstractMap) List(java.util.List) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) Stack(org.hibernate.internal.util.collections.Stack) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FetchParent(org.hibernate.sql.results.graph.FetchParent) Fetchable(org.hibernate.sql.results.graph.Fetchable) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) NavigablePath(org.hibernate.query.spi.NavigablePath) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) Fetch(org.hibernate.sql.results.graph.Fetch) AbstractMap(java.util.AbstractMap) Association(org.hibernate.metamodel.mapping.Association) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Aggregations

NonAggregatedIdentifierMapping (org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping)4 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)3 CompositeIdentifierMapping (org.hibernate.metamodel.mapping.CompositeIdentifierMapping)2 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)2 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)2 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Internal (org.hibernate.Internal)1 LockMode (org.hibernate.LockMode)1 TypeMismatchException (org.hibernate.TypeMismatchException)1 FetchTiming (org.hibernate.engine.FetchTiming)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 CollectionHelper (org.hibernate.internal.util.collections.CollectionHelper)1 Stack (org.hibernate.internal.util.collections.Stack)1 StandardStack (org.hibernate.internal.util.collections.StandardStack)1 Association (org.hibernate.metamodel.mapping.Association)1