Search in sources :

Example 1 with CompositeIdentifierMapping

use of org.hibernate.metamodel.mapping.CompositeIdentifierMapping 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 CompositeIdentifierMapping

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

the class AbstractEmbeddableInitializer method applyMapsId.

private void applyMapsId(RowProcessingState processingState) {
    final SharedSessionContractImplementor session = processingState.getSession();
    if (embedded instanceof CompositeIdentifierMapping) {
        final CompositeIdentifierMapping cid = (CompositeIdentifierMapping) embedded;
        final EmbeddableMappingType mappedIdEmbeddable = cid.getMappedIdEmbeddableTypeDescriptor();
        if (cid.hasContainingClass()) {
            final EmbeddableMappingType virtualIdEmbeddable = embedded.getEmbeddableTypeDescriptor();
            if (virtualIdEmbeddable == mappedIdEmbeddable) {
                return;
            }
            virtualIdEmbeddable.forEachAttributeMapping((position, virtualIdAttribute) -> {
                final AttributeMapping mappedIdAttribute = mappedIdEmbeddable.getAttributeMapping(position);
                if (virtualIdAttribute instanceof ToOneAttributeMapping && !(mappedIdAttribute instanceof ToOneAttributeMapping)) {
                    final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) virtualIdAttribute;
                    final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
                    final Object associationKey = fkDescriptor.getAssociationKeyFromSide(rowState[position], toOneAttributeMapping.getSideNature().inverse(), session);
                    rowState[position] = associationKey;
                }
            });
        }
    }
}
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) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType)

Example 3 with CompositeIdentifierMapping

use of org.hibernate.metamodel.mapping.CompositeIdentifierMapping 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

AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)3 CompositeIdentifierMapping (org.hibernate.metamodel.mapping.CompositeIdentifierMapping)3 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)2 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)2 NonAggregatedIdentifierMapping (org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping)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 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)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