Search in sources :

Example 6 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class LoadPlanBuilderTest method testCascadeBasedBuild.

@Test
public void testCascadeBasedBuild(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final EntityPersister entityDescriptor = (EntityPersister) sessionFactory.getRuntimeMetamodels().getEntityMappingType(Message.class);
    final SingleIdEntityLoaderStandardImpl<?> loader = new SingleIdEntityLoaderStandardImpl<>(entityDescriptor, sessionFactory);
    final LoadQueryInfluencers influencers = new LoadQueryInfluencers() {

        @Override
        public CascadingFetchProfile getEnabledCascadingFetchProfile() {
            return CascadingFetchProfile.MERGE;
        }
    };
    final SingleIdLoadPlan<?> loadPlan = loader.resolveLoadPlan(LockOptions.READ, influencers, sessionFactory);
    final List<DomainResult<?>> domainResults = loadPlan.getJdbcSelect().getJdbcValuesMappingProducer().resolve(null, sessionFactory).getDomainResults();
    assertThat(domainResults).hasSize(1);
    final DomainResult<?> domainResult = domainResults.get(0);
    assertThat(domainResult).isInstanceOf(EntityResult.class);
    final EntityResult entityResult = (EntityResult) domainResult;
    assertThat(entityResult.getFetches()).hasSize(2);
    final Fetch txtFetch = entityResult.getFetches().get(0);
    assertThat(txtFetch).isNotNull();
    assertThat(txtFetch.getFetchedMapping().getFetchableName()).isEqualTo("msgTxt");
    assertThat(txtFetch.getTiming()).isEqualTo(FetchTiming.IMMEDIATE);
    final Fetch posterFetch = entityResult.getFetches().get(1);
    assertThat(posterFetch).isNotNull();
    assertThat(posterFetch.getFetchedMapping().getFetchableName()).isEqualTo("poster");
    assertThat(posterFetch.getTiming()).isEqualTo(FetchTiming.IMMEDIATE);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) Fetch(org.hibernate.sql.results.graph.Fetch) SingleIdEntityLoaderStandardImpl(org.hibernate.loader.ast.internal.SingleIdEntityLoaderStandardImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) DomainResult(org.hibernate.sql.results.graph.DomainResult) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) Test(org.junit.jupiter.api.Test)

Example 7 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class DynamicResultBuilderEntityCalculated method buildResult.

@Override
public EntityResult buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
    final TableGroup tableGroup = entityMapping.createRootTableGroup(true, navigablePath, tableAlias, null, new SqlAliasBaseConstant(tableAlias), creationStateImpl, creationStateImpl.getFromClauseAccess(), creationStateImpl.getCreationContext());
    creationStateImpl.getFromClauseAccess().registerTableGroup(navigablePath, tableGroup);
    if (explicitLockMode != null) {
        domainResultCreationState.getSqlAstCreationState().registerLockMode(tableAlias, explicitLockMode);
    }
    return (EntityResult) entityMapping.createDomainResult(navigablePath, tableGroup, tableAlias, domainResultCreationState);
}
Also used : DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult)

Example 8 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class ImplicitModelPartResultBuilderEntity method buildResult.

@Override
public EntityResult buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
    creationStateImpl.disallowPositionalSelections();
    final TableGroup tableGroup = creationStateImpl.getFromClauseAccess().resolveTableGroup(navigablePath, np -> {
        if (navigablePath.getParent() != null) {
            return creationStateImpl.getFromClauseAccess().getTableGroup(navigablePath.getParent());
        }
        return modelPart.getEntityMappingType().createRootTableGroup(// since this is only used for result set mappings, the canUseInnerJoins value is irrelevant.
        true, navigablePath, null, null, creationStateImpl, creationStateImpl.getCreationContext());
    });
    return (EntityResult) modelPart.createDomainResult(navigablePath, tableGroup, null, domainResultCreationState);
}
Also used : DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult)

Example 9 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class ResultSetMappingImpl method resolve.

@Override
public JdbcValuesMapping resolve(JdbcValuesMetadata jdbcResultsMetadata, SessionFactoryImplementor sessionFactory) {
    final int numberOfResults;
    final int rowSize = jdbcResultsMetadata.getColumnCount();
    if (resultBuilders == null) {
        numberOfResults = rowSize;
    } else {
        numberOfResults = resultBuilders.size();
    }
    final List<SqlSelection> sqlSelections = new ArrayList<>(rowSize);
    final List<DomainResult<?>> domainResults = new ArrayList<>(numberOfResults);
    final DomainResultCreationStateImpl creationState = new DomainResultCreationStateImpl(mappingIdentifier, jdbcResultsMetadata, legacyFetchBuilders, sqlSelections::add, sessionFactory);
    for (int i = 0; i < numberOfResults; i++) {
        final ResultBuilder resultBuilder = resultBuilders != null ? resultBuilders.get(i) : null;
        final DomainResult<?> domainResult;
        if (resultBuilder == null) {
            domainResult = makeImplicitDomainResult(i, sqlSelections::add, jdbcResultsMetadata, sessionFactory);
        } else {
            domainResult = resultBuilder.buildResult(jdbcResultsMetadata, domainResults.size(), creationState.getLegacyFetchResolver()::resolve, creationState);
        }
        if (domainResult.containsAnyNonScalarResults()) {
            creationState.disallowPositionalSelections();
        }
        domainResults.add(domainResult);
    }
    // As people should be able to just run native queries and work with tuples
    if (resultBuilders != null) {
        final Set<String> knownDuplicateAliases = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        if (resultBuilders.size() == 1 && domainResults.size() == 1 && domainResults.get(0) instanceof EntityResult) {
            // Special case for result set mappings that just fetch a single polymorphic entity
            final EntityResult entityResult = (EntityResult) domainResults.get(0);
            final boolean polymorphic = entityResult.getReferencedMappingContainer().getEntityPersister().getEntityMetamodel().isPolymorphic();
            // If we don't do that, there is no way to fetch joined inheritance entities
            if (polymorphic && (legacyFetchBuilders == null || legacyFetchBuilders.isEmpty()) && !hasJoinFetches(entityResult.getFetches())) {
                final Set<String> aliases = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
                final AbstractEntityPersister entityPersister = (AbstractEntityPersister) entityResult.getReferencedMappingContainer().getEntityPersister();
                for (String[] columns : entityPersister.getContraintOrderedTableKeyColumnClosure()) {
                    addColumns(aliases, knownDuplicateAliases, columns);
                }
                addColumn(aliases, knownDuplicateAliases, entityPersister.getDiscriminatorColumnName());
                addColumn(aliases, knownDuplicateAliases, entityPersister.getVersionColumnName());
                for (int i = 0; i < entityPersister.countSubclassProperties(); i++) {
                    addColumns(aliases, knownDuplicateAliases, entityPersister.getSubclassPropertyColumnNames(i));
                }
            }
        }
        final String[] aliases = new String[rowSize];
        final Map<String, Boolean> aliasHasDuplicates = new HashMap<>(rowSize);
        for (int i = 0; i < rowSize; i++) {
            aliasHasDuplicates.compute(aliases[i] = jdbcResultsMetadata.resolveColumnName(i + 1), (k, v) -> v == null ? Boolean.FALSE : Boolean.TRUE);
        }
        // Only check for duplicates for the selections that we actually use
        for (SqlSelection sqlSelection : sqlSelections) {
            final String alias = aliases[sqlSelection.getValuesArrayPosition()];
            if (!knownDuplicateAliases.contains(alias) && aliasHasDuplicates.get(alias) == Boolean.TRUE) {
                throw new NonUniqueDiscoveredSqlAliasException("Encountered a duplicated sql alias [" + alias + "] during auto-discovery of a native-sql query");
            }
        }
    }
    return new JdbcValuesMappingImpl(sqlSelections, domainResults, rowSize, creationState.getRegisteredLockModes());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DomainResult(org.hibernate.sql.results.graph.DomainResult) NonUniqueDiscoveredSqlAliasException(org.hibernate.loader.NonUniqueDiscoveredSqlAliasException) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) TreeSet(java.util.TreeSet) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister)

Example 10 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class ScrollableResultsConsumer method containsCollectionFetches.

private boolean containsCollectionFetches(JdbcValuesMapping valuesMapping) {
    final List<DomainResult<?>> domainResults = valuesMapping.getDomainResults();
    for (DomainResult domainResult : domainResults) {
        if (domainResult instanceof EntityResult) {
            EntityResult entityResult = (EntityResult) domainResult;
            final List<Fetch> fetches = entityResult.getFetches();
            for (Fetch fetch : fetches) {
                if (fetch instanceof EagerCollectionFetch) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Fetch(org.hibernate.sql.results.graph.Fetch) EagerCollectionFetch(org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch) DomainResult(org.hibernate.sql.results.graph.DomainResult) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) EagerCollectionFetch(org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch)

Aggregations

EntityResult (org.hibernate.sql.results.graph.entity.EntityResult)16 DomainResult (org.hibernate.sql.results.graph.DomainResult)14 Fetch (org.hibernate.sql.results.graph.Fetch)13 DelayedCollectionFetch (org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch)10 EntityPersister (org.hibernate.persister.entity.EntityPersister)9 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)9 EntityFetch (org.hibernate.sql.results.graph.entity.EntityFetch)9 Test (org.junit.jupiter.api.Test)9 HashMap (java.util.HashMap)7 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)7 TableGroupJoin (org.hibernate.sql.ast.tree.from.TableGroupJoin)7 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)7 ElementCollection (jakarta.persistence.ElementCollection)6 Embeddable (jakarta.persistence.Embeddable)6 Embedded (jakarta.persistence.Embedded)6 Entity (jakarta.persistence.Entity)6 FetchType (jakarta.persistence.FetchType)6 Id (jakarta.persistence.Id)6 ManyToOne (jakarta.persistence.ManyToOne)6 OneToMany (jakarta.persistence.OneToMany)6