Search in sources :

Example 1 with EmbeddedAttributeMapping

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

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

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

the class AttributeOrderingTests method verifyRuntimeEntityMapping.

public void verifyRuntimeEntityMapping(EntityMappingType entityMappingType) {
    final NaturalIdMapping naturalIdMapping = entityMappingType.getNaturalIdMapping();
    assertThat(naturalIdMapping, notNullValue());
    assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
    assertThat(naturalIdMapping.getNaturalIdAttributes().get(0).getAttributeName(), is("assignment"));
    assertThat(naturalIdMapping.getNaturalIdAttributes().get(1).getAttributeName(), is("userCode"));
    final ArrayList<AttributeMapping> attributeMappings = new ArrayList<>(entityMappingType.getAttributeMappings());
    assertThat(attributeMappings.size(), is(5));
    assertThat(attributeMappings.get(0).getAttributeName(), is("assignment"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[0], is("assignment"));
    assertThat(attributeMappings.get(1).getAttributeName(), is("name"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[1], is("name"));
    final EmbeddedAttributeMapping theComponentAttrMapping = (EmbeddedAttributeMapping) attributeMappings.get(2);
    assertThat(theComponentAttrMapping.getAttributeName(), is("theComponent"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[2], is("theComponent"));
    final EmbeddableMappingType embeddable = theComponentAttrMapping.getMappedType();
    final ArrayList<AttributeMapping> embeddableAttributeMappings = new ArrayList<>(embeddable.getAttributeMappings());
    assertThat(embeddableAttributeMappings.get(0).getAttributeName(), is("nestedAnything"));
    assertThat(embeddableAttributeMappings.get(1).getAttributeName(), is("nestedName"));
    assertThat(attributeMappings.get(3).getAttributeName(), is("theComponents"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[3], is("theComponents"));
    assertThat(attributeMappings.get(4).getAttributeName(), is("userCode"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[4], is("userCode"));
}
Also used : EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) ArrayList(java.util.ArrayList) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType)

Example 4 with EmbeddedAttributeMapping

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

the class LoaderSelectBuilder method createFetchableBiConsumer.

private BiConsumer<Fetchable, Boolean> createFetchableBiConsumer(FetchParent fetchParent, QuerySpec querySpec, LoaderSqlAstCreationState creationState, List<Fetch> fetches) {
    return (fetchable, isKeyFetchable) -> {
        final NavigablePath fetchablePath;
        if (isKeyFetchable) {
            final EntityIdentifierMapping identifierMapping;
            if (fetchParent instanceof BiDirectionalFetch) {
                final BiDirectionalFetch parentAsBiDirectionalFetch = (BiDirectionalFetch) fetchParent;
                final Fetchable biDirectionalFetchedMapping = parentAsBiDirectionalFetch.getFetchedMapping();
                if (biDirectionalFetchedMapping instanceof EntityValuedFetchable) {
                    identifierMapping = ((EntityValuedFetchable) biDirectionalFetchedMapping).getEntityMappingType().getIdentifierMapping();
                } else {
                    identifierMapping = null;
                }
            } else {
                final FetchableContainer fetchableContainer = fetchParent.getReferencedMappingContainer();
                if (fetchableContainer instanceof EntityValuedModelPart) {
                    final EntityValuedModelPart entityValuedModelPart = (EntityValuedModelPart) fetchableContainer;
                    identifierMapping = entityValuedModelPart.getEntityMappingType().getIdentifierMapping();
                } else {
                    identifierMapping = null;
                }
            }
            if (identifierMapping != null) {
                fetchablePath = new EntityIdentifierNavigablePath(fetchParent.getNavigablePath(), attributeName(identifierMapping));
            } else {
                fetchablePath = fetchParent.resolveNavigablePath(fetchable);
            }
        } else {
            fetchablePath = fetchParent.resolveNavigablePath(fetchable);
        }
        FetchTiming fetchTiming = fetchable.getMappedFetchOptions().getTiming();
        boolean joined = fetchable.getMappedFetchOptions().getStyle() == FetchStyle.JOIN;
        boolean explicitFetch = false;
        EntityGraphTraversalState.TraversalResult traversalResult = null;
        final boolean isFetchablePluralAttributeMapping = fetchable instanceof PluralAttributeMapping;
        if (!(fetchable instanceof CollectionPart)) {
            // 'entity graph' takes precedence over 'fetch profile'
            if (entityGraphTraversalState != null) {
                traversalResult = entityGraphTraversalState.traverse(fetchParent, fetchable, isKeyFetchable);
                fetchTiming = traversalResult.getFetchTiming();
                joined = traversalResult.isJoined();
                explicitFetch = true;
            } else if (loadQueryInfluencers.hasEnabledFetchProfiles()) {
                // There is no point in checking the fetch profile if it can't affect this fetchable
                if (fetchTiming != FetchTiming.IMMEDIATE || fetchable.incrementFetchDepth()) {
                    final String fetchableRole = fetchable.getNavigableRole().getFullPath();
                    for (String enabledFetchProfileName : loadQueryInfluencers.getEnabledFetchProfileNames()) {
                        final FetchProfile enabledFetchProfile = creationContext.getSessionFactory().getFetchProfile(enabledFetchProfileName);
                        final org.hibernate.engine.profile.Fetch profileFetch = enabledFetchProfile.getFetchByRole(fetchableRole);
                        if (profileFetch != null) {
                            fetchTiming = FetchTiming.IMMEDIATE;
                            joined = joined || profileFetch.getStyle() == org.hibernate.engine.profile.Fetch.Style.JOIN;
                            explicitFetch = true;
                        }
                    }
                }
            } else if (loadQueryInfluencers.getEnabledCascadingFetchProfile() != null) {
                final CascadeStyle cascadeStyle = ((AttributeMapping) fetchable).getAttributeMetadataAccess().resolveAttributeMetadata(fetchable.findContainingEntityMapping()).getCascadeStyle();
                final CascadingAction cascadingAction = loadQueryInfluencers.getEnabledCascadingFetchProfile().getCascadingAction();
                if (cascadeStyle == null || cascadeStyle.doCascade(cascadingAction)) {
                    fetchTiming = FetchTiming.IMMEDIATE;
                    // In 5.x the CascadeEntityJoinWalker only join fetched the first collection fetch
                    if (isFetchablePluralAttributeMapping) {
                        joined = !hasCollectionJoinFetches;
                    } else {
                        joined = true;
                    }
                }
            }
        }
        final String previousBagRole = currentBagRole;
        final String bagRole;
        if (isFetchablePluralAttributeMapping && ((PluralAttributeMapping) fetchable).getMappedType().getCollectionSemantics() instanceof BagSemantics) {
            bagRole = fetchable.getNavigableRole().getNavigableName();
        } else {
            bagRole = null;
        }
        if (joined && previousBagRole != null && bagRole != null) {
            // Avoid join fetching multiple bags
            joined = false;
        }
        boolean changeFetchDepth = !(fetchable instanceof BasicValuedModelPart) && !(fetchable instanceof EmbeddedAttributeMapping) && !(fetchable instanceof CollectionPart);
        try {
            if (changeFetchDepth) {
                fetchDepth++;
            }
            // There is no need to check for circular fetches if this is an explicit fetch
            if (!explicitFetch && !creationState.isResolvingCircularFetch()) {
                final Fetch biDirectionalFetch = fetchable.resolveCircularFetch(fetchablePath, fetchParent, fetchTiming, creationState);
                if (biDirectionalFetch != null) {
                    fetches.add(biDirectionalFetch);
                    return;
                }
            }
            final Integer maximumFetchDepth = creationContext.getMaximumFetchDepth();
            if (maximumFetchDepth != null) {
                if (fetchDepth == maximumFetchDepth + 1) {
                    joined = false;
                } else if (fetchDepth > maximumFetchDepth + 1) {
                    if (!(fetchable instanceof BasicValuedModelPart) && !(fetchable instanceof EmbeddedAttributeMapping)) {
                        return;
                    }
                }
            }
            if (joined) {
                // For join fetches we remember the currentBagRole so that we can avoid multiple bag fetches
                if (bagRole != null) {
                    currentBagRole = bagRole;
                }
            } else {
                // For non-join fetches, we reset the currentBagRole and set it to the previous value in the finally block
                currentBagRole = null;
            }
            final Fetch fetch = fetchParent.generateFetchableFetch(fetchable, fetchablePath, fetchTiming, joined, null, creationState);
            if (fetch.getTiming() == FetchTiming.IMMEDIATE && isFetchablePluralAttributeMapping) {
                final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
                if (joined) {
                    hasCollectionJoinFetches = true;
                    final TableGroup joinTableGroup = creationState.getFromClauseAccess().getTableGroup(fetchablePath);
                    applyFiltering(querySpec, joinTableGroup, pluralAttributeMapping, creationState);
                    applyOrdering(querySpec, fetchablePath, pluralAttributeMapping, creationState);
                }
            }
            fetches.add(fetch);
        } finally {
            if (changeFetchDepth) {
                fetchDepth--;
            }
            // otherwise we could run into a multiple bag fetch situation
            if (!joined) {
                currentBagRole = previousBagRole;
            }
            if (entityGraphTraversalState != null && traversalResult != null) {
                entityGraphTraversalState.backtrack(traversalResult);
            }
        }
    };
}
Also used : Arrays(java.util.Arrays) CollectionFetch(org.hibernate.sql.results.graph.collection.internal.CollectionFetch) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) GraphSemantic(org.hibernate.graph.GraphSemantic) ResultsHelper.attributeName(org.hibernate.query.results.ResultsHelper.attributeName) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) CascadingAction(org.hibernate.engine.spi.CascadingAction) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) EntityResultImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultImpl) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) BagSemantics(org.hibernate.collection.spi.BagSemantics) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BiDirectionalFetch(org.hibernate.sql.results.graph.BiDirectionalFetch) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Map(java.util.Map) FetchProfile(org.hibernate.engine.profile.FetchProfile) JdbcParameterImpl(org.hibernate.sql.exec.internal.JdbcParameterImpl) ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) Fetchable(org.hibernate.sql.results.graph.Fetchable) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) LockOptions(org.hibernate.LockOptions) NavigablePath(org.hibernate.query.spi.NavigablePath) AliasCollector(org.hibernate.sql.ast.spi.AliasCollector) DomainResult(org.hibernate.sql.results.graph.DomainResult) Expression(org.hibernate.sql.ast.tree.expression.Expression) OrderByFragment(org.hibernate.metamodel.mapping.ordering.OrderByFragment) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) List(java.util.List) CascadeStyle(org.hibernate.engine.spi.CascadeStyle) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) SimpleForeignKeyDescriptor(org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Logger(org.jboss.logging.Logger) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) ArrayList(java.util.ArrayList) RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) Loader(org.hibernate.loader.ast.spi.Loader) BiConsumer(java.util.function.BiConsumer) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) Loadable(org.hibernate.loader.ast.spi.Loadable) Fetch(org.hibernate.sql.results.graph.Fetch) Consumer(java.util.function.Consumer) Restrictable(org.hibernate.metamodel.mapping.Restrictable) EntityGraphTraversalState(org.hibernate.sql.results.graph.EntityGraphTraversalState) FetchStyle(org.hibernate.engine.FetchStyle) StandardEntityGraphTraversalStateImpl(org.hibernate.sql.results.internal.StandardEntityGraphTraversalStateImpl) AbstractMap(java.util.AbstractMap) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) CascadeStyle(org.hibernate.engine.spi.CascadeStyle) CascadingAction(org.hibernate.engine.spi.CascadingAction) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) Fetchable(org.hibernate.sql.results.graph.Fetchable) NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) CollectionFetch(org.hibernate.sql.results.graph.collection.internal.CollectionFetch) BiDirectionalFetch(org.hibernate.sql.results.graph.BiDirectionalFetch) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) Fetch(org.hibernate.sql.results.graph.Fetch) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) BiDirectionalFetch(org.hibernate.sql.results.graph.BiDirectionalFetch) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) FetchProfile(org.hibernate.engine.profile.FetchProfile) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) FetchTiming(org.hibernate.engine.FetchTiming) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) BagSemantics(org.hibernate.collection.spi.BagSemantics)

Example 5 with EmbeddedAttributeMapping

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

the class SmokeTests method testSimpleEntity.

@Test
public void testSimpleEntity(SessionFactoryScope scope) {
    final EntityPersister entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(SimpleEntity.class);
    final JdbcTypeRegistry jdbcTypeRegistry = entityDescriptor.getFactory().getTypeConfiguration().getJdbcTypeRegistry();
    final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
    assertThat(identifierMapping.getMappedType().getMappedJavaType().getJavaTypeClass(), sameInstance(Integer.class));
    {
        final ModelPart namePart = entityDescriptor.findSubPart("name");
        assert namePart instanceof BasicAttributeMapping;
        assert "mapping_simple_entity".equals(((BasicAttributeMapping) namePart).getContainingTableExpression());
        assert "name".equals(((BasicAttributeMapping) namePart).getSelectionExpression());
    }
    {
        final ModelPart genderPart = entityDescriptor.findSubPart("gender");
        assert genderPart instanceof BasicAttributeMapping;
        final BasicAttributeMapping genderAttrMapping = (BasicAttributeMapping) genderPart;
        assert "mapping_simple_entity".equals(genderAttrMapping.getContainingTableExpression());
        assert "gender".equals(genderAttrMapping.getSelectionExpression());
        assertThat(genderAttrMapping.getJavaType().getJavaTypeClass(), equalTo(Gender.class));
        final BasicValueConverter valueConverter = genderAttrMapping.getValueConverter();
        assertThat(valueConverter, instanceOf(OrdinalEnumValueConverter.class));
        assertThat(valueConverter.getDomainJavaType(), is(genderAttrMapping.getJavaType()));
        assertThat(valueConverter.getRelationalJavaType().getJavaTypeClass(), equalTo(Integer.class));
        assertThat(genderAttrMapping.getJdbcMapping().getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.TINYINT)));
    }
    {
        final ModelPart part = entityDescriptor.findSubPart("gender2");
        assert part instanceof BasicAttributeMapping;
        final BasicAttributeMapping attrMapping = (BasicAttributeMapping) part;
        assert "mapping_simple_entity".equals(attrMapping.getContainingTableExpression());
        assert "gender2".equals(attrMapping.getSelectionExpression());
        assertThat(attrMapping.getJavaType().getJavaTypeClass(), equalTo(Gender.class));
        final BasicValueConverter valueConverter = attrMapping.getValueConverter();
        assertThat(valueConverter, instanceOf(NamedEnumValueConverter.class));
        assertThat(valueConverter.getDomainJavaType(), is(attrMapping.getJavaType()));
        assertThat(valueConverter.getRelationalJavaType().getJavaTypeClass(), equalTo(String.class));
        assertThat(attrMapping.getJdbcMapping().getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.VARCHAR)));
    }
    {
        final ModelPart part = entityDescriptor.findSubPart("gender3");
        assert part instanceof BasicAttributeMapping;
        final BasicAttributeMapping attrMapping = (BasicAttributeMapping) part;
        assert "mapping_simple_entity".equals(attrMapping.getContainingTableExpression());
        assert "gender3".equals(attrMapping.getSelectionExpression());
        assertThat(attrMapping.getJavaType().getJavaTypeClass(), equalTo(Gender.class));
        final BasicValueConverter valueConverter = attrMapping.getValueConverter();
        assertThat(valueConverter, instanceOf(JpaAttributeConverter.class));
        assertThat(valueConverter.getDomainJavaType(), is(attrMapping.getJavaType()));
        assertThat(valueConverter.getRelationalJavaType().getJavaTypeClass(), equalTo(Character.class));
        assertThat(attrMapping.getJdbcMapping().getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.CHAR)));
    }
    {
        final ModelPart part = entityDescriptor.findSubPart("component");
        assert part instanceof EmbeddedAttributeMapping;
        final EmbeddedAttributeMapping attrMapping = (EmbeddedAttributeMapping) part;
        assertThat(attrMapping.getContainingTableExpression(), is("mapping_simple_entity"));
        assertThat(attrMapping.getEmbeddableTypeDescriptor().getJdbcTypeCount(), is(4));
        assertThat(attrMapping.getEmbeddableTypeDescriptor().getSelectable(0).getSelectionExpression(), is("attribute1"));
        assertThat(attrMapping.getEmbeddableTypeDescriptor().getSelectable(1).getSelectionExpression(), is("attribute2"));
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) BasicValueConverter(org.hibernate.metamodel.model.convert.spi.BasicValueConverter) Test(org.junit.jupiter.api.Test)

Aggregations

EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)8 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)4 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)4 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)4 Test (org.junit.jupiter.api.Test)4 ArrayList (java.util.ArrayList)3 RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 List (java.util.List)2 FetchTiming (org.hibernate.engine.FetchTiming)2 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)2 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)2 ModelPart (org.hibernate.metamodel.mapping.ModelPart)2 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)2 EmbeddableRepresentationStrategy (org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy)2 AbstractMap (java.util.AbstractMap)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Objects (java.util.Objects)1