Search in sources :

Example 11 with QueryParameterBindings

use of org.hibernate.query.spi.QueryParameterBindings 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)

Example 12 with QueryParameterBindings

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

the class CollectionLoaderBatchKey method batchLoad.

private void batchLoad(Object[] batchIds, int numberOfIds, SharedSessionContractImplementor session) {
    if (log.isDebugEnabled()) {
        log.debugf("Batch loading collection [%s] : %s", getLoadable().getCollectionDescriptor().getRole(), batchIds);
    }
    int smallBatchStart = 0;
    int smallBatchLength = Math.min(numberOfIds, batchSize);
    while (true) {
        final List<JdbcParameter> jdbcParameters;
        final SelectStatement sqlAst;
        if (smallBatchLength == batchSize) {
            jdbcParameters = this.batchSizeJdbcParameters;
            sqlAst = this.batchSizeSqlAst;
        } else {
            jdbcParameters = new ArrayList<>();
            sqlAst = LoaderSelectBuilder.createSelect(getLoadable(), // null here means to select everything
            null, getLoadable().getKeyDescriptor(), null, numberOfIds, session.getLoadQueryInfluencers(), LockOptions.NONE, jdbcParameters::add, session.getFactory());
        }
        final SessionFactoryImplementor sessionFactory = session.getFactory();
        final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
        final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
        final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
        final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(null, QueryOptions.NONE);
        final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(keyJdbcCount * smallBatchLength);
        jdbcSelect.bindFilterJdbcParameters(jdbcParameterBindings);
        int offset = 0;
        for (int i = smallBatchStart; i < smallBatchStart + smallBatchLength; i++) {
            offset += jdbcParameterBindings.registerParametersForEachJdbcValue(batchIds[i], Clause.WHERE, offset, getLoadable().getKeyDescriptor(), jdbcParameters, session);
        }
        assert offset == jdbcParameters.size();
        final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(session.getPersistenceContext().getBatchFetchQueue(), sqlAst, Collections.emptyList(), jdbcParameterBindings);
        jdbcServices.getJdbcSelectExecutor().list(jdbcSelect, jdbcParameterBindings, 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 void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) {
                subSelectFetchableKeysHandler.addKey(entityKey, entry);
            }

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

            @Override
            public Callback getCallback() {
                return null;
            }
        }, RowTransformerPassThruImpl.instance(), ListResultsConsumer.UniqueSemantic.FILTER);
        // prepare for the next round...
        smallBatchStart += smallBatchLength;
        if (smallBatchStart >= numberOfIds) {
            break;
        }
        smallBatchLength = Math.min(numberOfIds - smallBatchStart, batchSize);
    }
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) 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) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) EntityKey(org.hibernate.engine.spi.EntityKey) 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) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry)

Example 13 with QueryParameterBindings

use of org.hibernate.query.spi.QueryParameterBindings 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 14 with QueryParameterBindings

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

the class NativeSelectQueryPlanImpl method performScroll.

@Override
public ScrollableResultsImplementor<R> performScroll(ScrollMode scrollMode, DomainQueryExecutionContext executionContext) {
    if (executionContext.getQueryOptions().getEffectiveLimit().getMaxRowsJpa() == 0) {
        return EmptyScrollableResults.INSTANCE;
    }
    final List<JdbcParameterBinder> jdbcParameterBinders;
    final JdbcParameterBindings jdbcParameterBindings;
    final QueryParameterBindings queryParameterBindings = executionContext.getQueryParameterBindings();
    if (parameterList == null || parameterList.isEmpty()) {
        jdbcParameterBinders = Collections.emptyList();
        jdbcParameterBindings = JdbcParameterBindings.NO_BINDINGS;
    } else {
        jdbcParameterBinders = new ArrayList<>(parameterList.size());
        jdbcParameterBindings = new JdbcParameterBindingsImpl(parameterList.size());
        jdbcParameterBindings.registerNativeQueryParameters(queryParameterBindings, parameterList, jdbcParameterBinders, executionContext.getSession().getFactory());
    }
    final JdbcSelect jdbcSelect = new JdbcSelect(sql, jdbcParameterBinders, resultSetMapping, affectedTableNames, Collections.emptySet());
    final JdbcSelectExecutor executor = JdbcSelectExecutorStandardImpl.INSTANCE;
    // return jdbcServices.getJdbcSelectExecutor().scroll(
    return executor.scroll(jdbcSelect, scrollMode, jdbcParameterBindings, SqmJdbcExecutionContextAdapter.usingLockingAndPaging(executionContext), null);
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBinder(org.hibernate.sql.exec.spi.JdbcParameterBinder) JdbcSelectExecutor(org.hibernate.sql.exec.spi.JdbcSelectExecutor) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 15 with QueryParameterBindings

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

the class NativeSelectQueryPlanImpl method performList.

@Override
public List<R> performList(DomainQueryExecutionContext executionContext) {
    if (executionContext.getQueryOptions().getEffectiveLimit().getMaxRowsJpa() == 0) {
        return Collections.emptyList();
    }
    final List<JdbcParameterBinder> jdbcParameterBinders;
    final JdbcParameterBindings jdbcParameterBindings;
    final QueryParameterBindings queryParameterBindings = executionContext.getQueryParameterBindings();
    if (parameterList == null || parameterList.isEmpty()) {
        jdbcParameterBinders = Collections.emptyList();
        jdbcParameterBindings = JdbcParameterBindings.NO_BINDINGS;
    } else {
        jdbcParameterBinders = new ArrayList<>(parameterList.size());
        jdbcParameterBindings = new JdbcParameterBindingsImpl(parameterList.size());
        jdbcParameterBindings.registerNativeQueryParameters(queryParameterBindings, parameterList, jdbcParameterBinders, executionContext.getSession().getFactory());
    }
    executionContext.getSession().autoFlushIfRequired(affectedTableNames);
    final JdbcSelect jdbcSelect = new JdbcSelect(sql, jdbcParameterBinders, resultSetMapping, affectedTableNames, Collections.emptySet());
    final JdbcSelectExecutor executor = JdbcSelectExecutorStandardImpl.INSTANCE;
    // return jdbcServices.getJdbcSelectExecutor().execute(
    return executor.list(jdbcSelect, jdbcParameterBindings, SqmJdbcExecutionContextAdapter.usingLockingAndPaging(executionContext), null, ListResultsConsumer.UniqueSemantic.NONE);
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBinder(org.hibernate.sql.exec.spi.JdbcParameterBinder) JdbcSelectExecutor(org.hibernate.sql.exec.spi.JdbcSelectExecutor) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Aggregations

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