Search in sources :

Example 1 with PluralAttributeMapping

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

the class LoadPlanBuilderTest method testCollectionInitializerCase.

@Test
public void testCollectionInitializerCase(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final EntityPersister posterEntityDescriptor = sessionFactory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(Poster.class);
    final PluralAttributeMapping messages = (PluralAttributeMapping) posterEntityDescriptor.findAttributeMapping("messages");
    final CollectionLoaderSingleKey loader = new CollectionLoaderSingleKey(messages, LoadQueryInfluencers.NONE, sessionFactory);
    assertThat(loader.getSqlAst().getDomainResultDescriptors()).hasSize(1);
    assertThat(loader.getSqlAst().getDomainResultDescriptors().get(0)).isInstanceOf(CollectionDomainResult.class);
    final CollectionDomainResult domainResult = (CollectionDomainResult) loader.getSqlAst().getDomainResultDescriptors().get(0);
    DomainResultGraphPrinter.logDomainResultGraph(loader.getSqlAst().getDomainResultDescriptors());
    assertThat(domainResult.getFetches()).isEmpty();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) CollectionLoaderSingleKey(org.hibernate.loader.ast.internal.CollectionLoaderSingleKey) Test(org.junit.jupiter.api.Test)

Example 2 with PluralAttributeMapping

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

the class PluralAttributeMappingTests method testMaps.

@Test
public void testMaps(SessionFactoryScope scope) {
    final MappingMetamodel domainModel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor(EntityOfMaps.class);
    assertThat(containerEntityDescriptor.getNumberOfAttributeMappings(), is(17));
    final PluralAttributeMapping basicByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByBasic");
    assertThat(basicByBasic, notNullValue());
    assertThat(basicByBasic.getKeyDescriptor(), notNullValue());
    assertThat(basicByBasic.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping sortedBasicByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("sortedBasicByBasic");
    assertThat(sortedBasicByBasic, notNullValue());
    assertThat(sortedBasicByBasic.getKeyDescriptor(), notNullValue());
    assertThat(sortedBasicByBasic.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping sortedBasicByBasicWithSortNaturalByDefault = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("sortedBasicByBasicWithSortNaturalByDefault");
    assertThat(sortedBasicByBasicWithSortNaturalByDefault, notNullValue());
    assertThat(sortedBasicByBasicWithSortNaturalByDefault.getKeyDescriptor(), notNullValue());
    assertThat(sortedBasicByBasicWithSortNaturalByDefault.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping basicByEnum = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByEnum");
    assertThat(basicByEnum, notNullValue());
    assertThat(basicByEnum.getKeyDescriptor(), notNullValue());
    assertThat(basicByEnum.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping basicByConvertedEnum = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByConvertedEnum");
    assertThat(basicByConvertedEnum, notNullValue());
    assertThat(basicByConvertedEnum.getKeyDescriptor(), notNullValue());
    assertThat(basicByConvertedEnum.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping someStuffByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("componentByBasic");
    assertThat(someStuffByBasic, notNullValue());
    assertThat(someStuffByBasic.getKeyDescriptor(), notNullValue());
    assertThat(someStuffByBasic.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping basicBySomeStuff = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByComponent");
    assertThat(basicBySomeStuff, notNullValue());
    assertThat(basicBySomeStuff.getKeyDescriptor(), notNullValue());
    assertThat(basicBySomeStuff.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping oneToManyByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("oneToManyByBasic");
    assertThat(oneToManyByBasic, notNullValue());
    assertThat(oneToManyByBasic.getKeyDescriptor(), notNullValue());
    assertThat(oneToManyByBasic.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping basicByOneToMany = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByOneToMany");
    assertThat(basicByOneToMany, notNullValue());
    assertThat(basicByOneToMany.getKeyDescriptor(), notNullValue());
    assertThat(basicByOneToMany.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping manyToManyByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("manyToManyByBasic");
    assertThat(manyToManyByBasic, notNullValue());
    assertThat(manyToManyByBasic.getKeyDescriptor(), notNullValue());
    assertThat(manyToManyByBasic.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping sortedManyToManyByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("sortedManyToManyByBasic");
    assertThat(sortedManyToManyByBasic, notNullValue());
    assertThat(sortedManyToManyByBasic.getKeyDescriptor(), notNullValue());
    assertThat(sortedManyToManyByBasic.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping sortedManyToManyByBasicWithSortNaturalByDefault = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("sortedManyToManyByBasicWithSortNaturalByDefault");
    assertThat(sortedManyToManyByBasicWithSortNaturalByDefault, notNullValue());
    assertThat(sortedManyToManyByBasicWithSortNaturalByDefault.getKeyDescriptor(), notNullValue());
    assertThat(sortedManyToManyByBasicWithSortNaturalByDefault.getElementDescriptor(), notNullValue());
    final PluralAttributeMapping componentByBasicOrdered = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("componentByBasicOrdered");
    assertThat(componentByBasicOrdered, notNullValue());
    assertThat(componentByBasicOrdered.getKeyDescriptor(), notNullValue());
    assertThat(componentByBasicOrdered.getElementDescriptor(), notNullValue());
    assertThat(componentByBasicOrdered.getOrderByFragment(), notNullValue());
}
Also used : MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test)

Example 3 with PluralAttributeMapping

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

the class VirtualIdEmbeddable method forEachJdbcValue.

@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
    int span = 0;
    for (int i = 0; i < attributeMappings.size(); i++) {
        final AttributeMapping attributeMapping = attributeMappings.get(i);
        if (attributeMapping instanceof PluralAttributeMapping) {
            continue;
        }
        final Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
        span += attributeMapping.forEachJdbcValue(o, clause, span + offset, valuesConsumer, session);
    }
    return span;
}
Also used : PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping)

Example 4 with PluralAttributeMapping

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

the class IdClassEmbeddable method forEachJdbcValue.

@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
    int span = 0;
    for (int i = 0; i < attributeMappings.size(); i++) {
        final AttributeMapping attributeMapping = attributeMappings.get(i);
        if (attributeMapping instanceof PluralAttributeMapping) {
            continue;
        }
        final Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
        span += attributeMapping.forEachJdbcValue(o, clause, span + offset, valuesConsumer, session);
    }
    return span;
}
Also used : PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping)

Example 5 with PluralAttributeMapping

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

Aggregations

PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)40 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)23 NavigablePath (org.hibernate.query.spi.NavigablePath)16 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)15 ArrayList (java.util.ArrayList)12 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)12 TableGroupJoin (org.hibernate.sql.ast.tree.from.TableGroupJoin)11 CollectionPart (org.hibernate.metamodel.mapping.CollectionPart)9 ModelPart (org.hibernate.metamodel.mapping.ModelPart)9 CorrelatedPluralTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup)9 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)9 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)9 QueryPartTableGroup (org.hibernate.sql.ast.tree.from.QueryPartTableGroup)9 VirtualTableGroup (org.hibernate.sql.ast.tree.from.VirtualTableGroup)9 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)9 FetchTiming (org.hibernate.engine.FetchTiming)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)7 SqlAstQueryPartProcessingStateImpl (org.hibernate.query.sqm.sql.internal.SqlAstQueryPartProcessingStateImpl)7 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)7 Map (java.util.Map)6