Search in sources :

Example 16 with QueryOptions

use of org.hibernate.query.spi.QueryOptions in project hibernate-orm by hibernate.

the class ListResultsConsumer method consume.

@Override
public List<R> consume(JdbcValues jdbcValues, SharedSessionContractImplementor session, JdbcValuesSourceProcessingOptions processingOptions, JdbcValuesSourceProcessingStateStandardImpl jdbcValuesSourceProcessingState, RowProcessingStateStandardImpl rowProcessingState, RowReader<R> rowReader) {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final TypeConfiguration typeConfiguration = session.getTypeConfiguration();
    final QueryOptions queryOptions = rowProcessingState.getQueryOptions();
    RuntimeException ex = null;
    try {
        persistenceContext.getLoadContexts().register(jdbcValuesSourceProcessingState);
        final List<R> results = new ArrayList<>();
        final JavaType<R> domainResultJavaType = resolveDomainResultJavaType(rowReader.getDomainResultResultJavaType(), rowReader.getResultJavaTypes(), typeConfiguration);
        final ResultHandler<R> resultHandlerToUse;
        if (uniqueSemantic == UniqueSemantic.ALLOW && domainResultJavaType instanceof EntityJavaType) {
            resultHandlerToUse = ListResultsConsumer::deDuplicationHandling;
        } else {
            resultHandlerToUse = this.resultHandler;
        }
        while (rowProcessingState.next()) {
            final R row = rowReader.readRow(rowProcessingState, processingOptions);
            resultHandlerToUse.handle(row, domainResultJavaType, results, rowProcessingState);
            rowProcessingState.finishRowProcessing();
        }
        try {
            jdbcValuesSourceProcessingState.finishUp();
        } finally {
            persistenceContext.getLoadContexts().deregister(jdbcValuesSourceProcessingState);
        }
        // noinspection unchecked
        final ResultListTransformer<R> resultListTransformer = (ResultListTransformer<R>) queryOptions.getResultListTransformer();
        if (resultListTransformer != null) {
            return resultListTransformer.transformList(results);
        }
        return results;
    } catch (RuntimeException e) {
        ex = e;
    } finally {
        try {
            rowReader.finishUp(jdbcValuesSourceProcessingState);
            jdbcValues.finishUp(session);
            persistenceContext.initializeNonLazyCollections();
        } catch (RuntimeException e) {
            if (ex != null) {
                ex.addSuppressed(e);
            } else {
                ex = e;
            }
        } finally {
            if (ex != null) {
                throw ex;
            }
        }
    }
    throw new IllegalStateException("Should not reach this!");
}
Also used : ArrayList(java.util.ArrayList) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) QueryOptions(org.hibernate.query.spi.QueryOptions) EntityJavaType(org.hibernate.type.descriptor.java.spi.EntityJavaType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) ResultListTransformer(org.hibernate.query.ResultListTransformer)

Example 17 with QueryOptions

use of org.hibernate.query.spi.QueryOptions in project hibernate-orm by hibernate.

the class GeneratedValuesProcessor method processGeneratedValues.

public void processGeneratedValues(Object entity, Object id, Object[] state, SharedSessionContractImplementor session) {
    if (selectStatement == null) {
        return;
    }
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
    final JdbcParameterBindings jdbcParamBindings = new JdbcParameterBindingsImpl(jdbcParameters.size());
    int offset = jdbcParamBindings.registerParametersForEachJdbcValue(id, Clause.WHERE, entityDescriptor.getIdentifierMapping(), jdbcParameters, session);
    assert offset == jdbcParameters.size();
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, selectStatement).translate(jdbcParamBindings, QueryOptions.NONE);
    final List<Object[]> results = session.getFactory().getJdbcServices().getJdbcSelectExecutor().list(jdbcSelect, jdbcParamBindings, new ExecutionContext() {

        @Override
        public SharedSessionContractImplementor getSession() {
            return session;
        }

        @Override
        public QueryOptions getQueryOptions() {
            return QueryOptions.NONE;
        }

        @Override
        public String getQueryIdentifier(String sql) {
            return sql;
        }

        @Override
        public QueryParameterBindings getQueryParameterBindings() {
            return QueryParameterBindings.NO_PARAM_BINDINGS;
        }

        @Override
        public Callback getCallback() {
            throw new UnsupportedMappingException("Follow-on locking not supported yet");
        }
    }, (row) -> row, ListResultsConsumer.UniqueSemantic.FILTER);
    assert results.size() == 1;
    final Object[] dbSelectionResults = results.get(0);
    for (int i = 0; i < valueDescriptors.size(); i++) {
        final GeneratedValueDescriptor descriptor = valueDescriptors.get(i);
        final Object generatedValue = descriptor.resolver.resolveGeneratedValue(dbSelectionResults, entity, session);
        state[descriptor.attribute.getStateArrayPosition()] = generatedValue;
        descriptor.attribute.getAttributeMetadataAccess().resolveAttributeMetadata(entityDescriptor).getPropertyAccess().getSetter().set(entity, generatedValue);
    }
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) QueryOptions(org.hibernate.query.spi.QueryOptions) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) UnsupportedMappingException(org.hibernate.metamodel.UnsupportedMappingException) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 18 with QueryOptions

use of org.hibernate.query.spi.QueryOptions in project hibernate-orm by hibernate.

the class SingleUniqueKeyEntityLoaderStandard method load.

@Override
public T load(Object ukValue, LockOptions lockOptions, Boolean readOnly, SharedSessionContractImplementor session) {
    final SessionFactoryImplementor sessionFactory = session.getFactory();
    // todo (6.0) : cache the SQL AST and JdbcParameters
    final List<JdbcParameter> jdbcParameters = new ArrayList<>();
    final SelectStatement sqlAst = LoaderSelectBuilder.createSelectByUniqueKey(entityDescriptor, Collections.emptyList(), uniqueKeyAttribute, null, 1, LoadQueryInfluencers.NONE, LockOptions.NONE, jdbcParameters::add, sessionFactory);
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
    final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(jdbcParameters.size());
    int offset = jdbcParameterBindings.registerParametersForEachJdbcValue(ukValue, Clause.WHERE, uniqueKeyAttribute, jdbcParameters, session);
    assert offset == jdbcParameters.size();
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(jdbcParameterBindings, QueryOptions.NONE);
    final List<Object> list = sessionFactory.getJdbcServices().getJdbcSelectExecutor().list(jdbcSelect, jdbcParameterBindings, new ExecutionContext() {

        private final Callback callback = new CallbackImpl();

        @Override
        public SharedSessionContractImplementor getSession() {
            return session;
        }

        @Override
        public QueryOptions getQueryOptions() {
            return new QueryOptionsAdapter() {

                @Override
                public Boolean isReadOnly() {
                    return readOnly;
                }
            };
        }

        @Override
        public String getQueryIdentifier(String sql) {
            return sql;
        }

        @Override
        public QueryParameterBindings getQueryParameterBindings() {
            return QueryParameterBindings.NO_PARAM_BINDINGS;
        }

        @Override
        public Callback getCallback() {
            return callback;
        }
    }, row -> row[0], ListResultsConsumer.UniqueSemantic.FILTER);
    switch(list.size()) {
        case 0:
            return null;
        case 1:
            // noinspection unchecked
            return (T) list.get(0);
    }
    throw new HibernateException("More than one row with the given identifier was found: " + ukValue + ", for class: " + entityDescriptor.getEntityName());
}
Also used : CallbackImpl(org.hibernate.sql.exec.internal.CallbackImpl) ArrayList(java.util.ArrayList) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) QueryOptions(org.hibernate.query.spi.QueryOptions) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) QueryOptionsAdapter(org.hibernate.query.spi.QueryOptionsAdapter) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Aggregations

QueryOptions (org.hibernate.query.spi.QueryOptions)18 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)13 ExecutionContext (org.hibernate.sql.exec.spi.ExecutionContext)13 QueryParameterBindings (org.hibernate.query.spi.QueryParameterBindings)12 Callback (org.hibernate.sql.exec.spi.Callback)12 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)11 JdbcParameterBindingsImpl (org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl)11 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)11 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)10 SqlAstTranslatorFactory (org.hibernate.sql.ast.SqlAstTranslatorFactory)10 JdbcSelect (org.hibernate.sql.exec.spi.JdbcSelect)10 ArrayList (java.util.ArrayList)8 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)8 HibernateException (org.hibernate.HibernateException)5 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)5 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)5 EntityKey (org.hibernate.engine.spi.EntityKey)4 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)4 LoadingEntityEntry (org.hibernate.sql.results.graph.entity.LoadingEntityEntry)4 CollectionKey (org.hibernate.engine.spi.CollectionKey)3