Search in sources :

Example 11 with DomainResult

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

the class JdbcSelectExecutorStandardImpl method doExecuteQuery.

private <T, R> T doExecuteQuery(JdbcSelect jdbcSelect, JdbcParameterBindings jdbcParameterBindings, ExecutionContext executionContext, RowTransformer<R> rowTransformer, Function<String, PreparedStatement> statementCreator, ResultsConsumer<T, R> resultsConsumer) {
    final DeferredResultSetAccess deferredResultSetAccess = new DeferredResultSetAccess(jdbcSelect, jdbcParameterBindings, executionContext, statementCreator);
    final JdbcValues jdbcValues = resolveJdbcValuesSource(executionContext.getQueryIdentifier(deferredResultSetAccess.getFinalSql()), jdbcSelect, resultsConsumer.canResultsBeCached(), executionContext, deferredResultSetAccess);
    if (rowTransformer == null) {
        @SuppressWarnings("unchecked") final TupleTransformer<R> tupleTransformer = (TupleTransformer<R>) executionContext.getQueryOptions().getTupleTransformer();
        if (tupleTransformer == null) {
            rowTransformer = RowTransformerPassThruImpl.instance();
        } else {
            final List<DomainResult<?>> domainResults = jdbcValues.getValuesMapping().getDomainResults();
            final String[] aliases = new String[domainResults.size()];
            for (int i = 0; i < domainResults.size(); i++) {
                aliases[i] = domainResults.get(i).getResultVariable();
            }
            rowTransformer = new RowTransformerTupleTransformerAdapter<>(aliases, tupleTransformer);
        }
    }
    final boolean stats;
    long startTime = 0;
    final StatisticsImplementor statistics = executionContext.getSession().getFactory().getStatistics();
    if (executionContext.hasQueryExecutionToBeAddedToStatistics() && jdbcValues instanceof JdbcValuesResultSetImpl) {
        stats = statistics.isStatisticsEnabled();
        if (stats) {
            startTime = System.nanoTime();
        }
    } else {
        stats = false;
    }
    /*
		 * Processing options effectively are only used for entity loading.  Here we don't need these values.
		 */
    final JdbcValuesSourceProcessingOptions processingOptions = new JdbcValuesSourceProcessingOptions() {

        @Override
        public Object getEffectiveOptionalObject() {
            return executionContext.getEntityInstance();
        }

        @Override
        public String getEffectiveOptionalEntityName() {
            return null;
        }

        @Override
        public Object getEffectiveOptionalId() {
            return executionContext.getEntityId();
        }

        @Override
        public boolean shouldReturnProxies() {
            return true;
        }
    };
    final JdbcValuesSourceProcessingStateStandardImpl valuesProcessingState = new JdbcValuesSourceProcessingStateStandardImpl(executionContext, processingOptions, executionContext::registerLoadingEntityEntry);
    final RowReader<R> rowReader = ResultsHelper.createRowReader(executionContext, // because the EntityEntrys would already have the desired lock mode
    deferredResultSetAccess.usesFollowOnLocking() ? LockOptions.NONE : executionContext.getQueryOptions().getLockOptions(), rowTransformer, jdbcValues);
    final RowProcessingStateStandardImpl rowProcessingState = new RowProcessingStateStandardImpl(valuesProcessingState, executionContext, rowReader, jdbcValues);
    final T result = resultsConsumer.consume(jdbcValues, executionContext.getSession(), processingOptions, valuesProcessingState, rowProcessingState, rowReader);
    if (stats) {
        final long endTime = System.nanoTime();
        final long milliseconds = TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS);
        statistics.queryExecuted(executionContext.getQueryIdentifier(jdbcSelect.getSql()), getResultSize(result), milliseconds);
    }
    return result;
}
Also used : JdbcValuesSourceProcessingOptions(org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingOptions) JdbcValues(org.hibernate.sql.results.jdbc.spi.JdbcValues) DeferredResultSetAccess(org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess) DomainResult(org.hibernate.sql.results.graph.DomainResult) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) JdbcValuesResultSetImpl(org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl) TupleTransformer(org.hibernate.query.TupleTransformer) JdbcValuesSourceProcessingStateStandardImpl(org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl) RowProcessingStateStandardImpl(org.hibernate.sql.results.internal.RowProcessingStateStandardImpl)

Example 12 with DomainResult

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

the class SimpleForeignKeyDescriptor method createDomainResult.

private <T> DomainResult<T> createDomainResult(NavigablePath navigablePath, TableGroup tableGroup, SelectableMapping selectableMapping, DomainResultCreationState creationState) {
    final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final TableReference tableReference = tableGroup.resolveTableReference(navigablePath.append(getTargetPart().getFetchableName()), selectableMapping.getContainingTableExpression());
    final String identificationVariable = tableReference.getIdentificationVariable();
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selectableMapping.getSelectionExpression()), s -> new ColumnReference(identificationVariable, selectableMapping, creationState.getSqlAstCreationState().getCreationContext().getSessionFactory())), selectableMapping.getJdbcMapping().getJavaTypeDescriptor(), sqlAstCreationState.getCreationContext().getSessionFactory().getTypeConfiguration());
    // noinspection unchecked
    return new BasicResult<T>(sqlSelection.getValuesArrayPosition(), null, selectableMapping.getJdbcMapping().getJavaTypeDescriptor());
}
Also used : BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JavaType(org.hibernate.type.descriptor.java.JavaType) Clause(org.hibernate.sql.ast.Clause) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) MappingType(org.hibernate.metamodel.mapping.MappingType) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) HibernateProxy(org.hibernate.proxy.HibernateProxy) FetchOptions(org.hibernate.sql.results.graph.FetchOptions) BiConsumer(java.util.function.BiConsumer) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) TableGroupProducer(org.hibernate.sql.ast.tree.from.TableGroupProducer) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) IntFunction(java.util.function.IntFunction) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) AssociationKey(org.hibernate.metamodel.mapping.AssociationKey) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResult(org.hibernate.sql.results.graph.DomainResult) Expression(org.hibernate.sql.ast.tree.expression.Expression) PropertyBasedMapping(org.hibernate.metamodel.mapping.PropertyBasedMapping) Fetch(org.hibernate.sql.results.graph.Fetch) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) FetchStyle(org.hibernate.engine.FetchStyle) List(java.util.List) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) Collections(java.util.Collections) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) IndexedConsumer(org.hibernate.mapping.IndexedConsumer) FetchParent(org.hibernate.sql.results.graph.FetchParent) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 13 with DomainResult

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

the class DynamicResultBuilderAttribute method buildResult.

@Override
public DomainResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    // todo (6.0) : TableGroups + `attributeMapping#buldResult`
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(columnAlias, state -> {
        final int resultSetPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
        final int valuesArrayPosition = resultSetPosition - 1;
        return new ResultSetMappingSqlSelection(valuesArrayPosition, attributeMapping);
    }), attributeMapping.getJavaType(), null, domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), columnAlias, attributeMapping.getJavaType(), attributeMapping.getValueConverter());
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) Locale(java.util.Locale) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) BiFunction(java.util.function.BiFunction) NativeQuery(org.hibernate.query.NativeQuery) DomainResult(org.hibernate.sql.results.graph.DomainResult) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 14 with DomainResult

use of org.hibernate.sql.results.graph.DomainResult 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)

Example 15 with DomainResult

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

the class EntityRowIdMappingImpl method createDomainResult.

@Override
public <T> DomainResult<T> createDomainResult(NavigablePath navigablePath, TableGroup tableGroup, String resultVariable, DomainResultCreationState creationState) {
    final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final TableReference columnTableReference = tableGroup.resolveTableReference(navigablePath, tableExpression);
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(columnTableReference, rowIdName), sqlAstProcessingState -> new ColumnReference(columnTableReference, rowIdName, false, // having to write a Dialect
    null, null, rowIdType, sqlAstCreationState.getCreationContext().getSessionFactory())), rowIdType.getJavaTypeDescriptor(), null, sqlAstCreationState.getCreationContext().getSessionFactory().getTypeConfiguration());
    return new BasicResult(sqlSelection.getValuesArrayPosition(), resultVariable, rowIdType.getJavaTypeDescriptor(), navigablePath);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicType(org.hibernate.type.BasicType) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) DomainResult(org.hibernate.sql.results.graph.DomainResult) JavaType(org.hibernate.type.descriptor.java.JavaType) Clause(org.hibernate.sql.ast.Clause) NavigablePath(org.hibernate.spi.NavigablePath) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) MappingType(org.hibernate.metamodel.mapping.MappingType) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) TableReference(org.hibernate.sql.ast.tree.from.TableReference) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) EntityRowIdMapping(org.hibernate.metamodel.mapping.EntityRowIdMapping) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) BiConsumer(java.util.function.BiConsumer) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) IndexedConsumer(org.hibernate.mapping.IndexedConsumer) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

DomainResult (org.hibernate.sql.results.graph.DomainResult)22 Fetch (org.hibernate.sql.results.graph.Fetch)11 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)9 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)8 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)7 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)7 EntityResult (org.hibernate.sql.results.graph.entity.EntityResult)7 BiConsumer (java.util.function.BiConsumer)6 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)6 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)6 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)6 DomainResultCreationState (org.hibernate.sql.results.graph.DomainResultCreationState)6 BasicResult (org.hibernate.sql.results.graph.basic.BasicResult)6 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)5 IndexedConsumer (org.hibernate.mapping.IndexedConsumer)5 MappingType (org.hibernate.metamodel.mapping.MappingType)5 NavigableRole (org.hibernate.metamodel.model.domain.NavigableRole)5 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 NavigablePath (org.hibernate.spi.NavigablePath)5 Clause (org.hibernate.sql.ast.Clause)5