Search in sources :

Example 1 with SimpleQueryOptions

use of org.hibernate.query.internal.SimpleQueryOptions in project hibernate-orm by hibernate.

the class SingleIdLoadPlan method load.

public T load(Object restrictedValue, Object entityInstance, Boolean readOnly, Boolean singleResultExpected, SharedSessionContractImplementor session) {
    final int jdbcTypeCount = restrictivePart.getJdbcTypeCount();
    assert jdbcParameters.size() % jdbcTypeCount == 0;
    final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(jdbcTypeCount);
    jdbcSelect.bindFilterJdbcParameters(jdbcParameterBindings);
    int offset = 0;
    while (offset < jdbcParameters.size()) {
        offset += jdbcParameterBindings.registerParametersForEachJdbcValue(restrictedValue, Clause.WHERE, offset, restrictivePart, jdbcParameters, session);
    }
    assert offset == jdbcParameters.size();
    final QueryOptions queryOptions = new SimpleQueryOptions(lockOptions, readOnly);
    final Callback callback = new CallbackImpl();
    final List<T> list = JdbcSelectExecutorStandardImpl.INSTANCE.list(jdbcSelect, jdbcParameterBindings, new ExecutionContext() {

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

        @Override
        public Object getEntityInstance() {
            return entityInstance;
        }

        @Override
        public Object getEntityId() {
            return restrictedValue;
        }

        @Override
        public QueryOptions getQueryOptions() {
            return queryOptions;
        }

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

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

        @Override
        public Callback getCallback() {
            return callback;
        }
    }, getRowTransformer(), singleResultExpected ? ListResultsConsumer.UniqueSemantic.ASSERT : ListResultsConsumer.UniqueSemantic.FILTER);
    if (list.isEmpty()) {
        return null;
    }
    final T entity = list.get(0);
    if (persister != null) {
        callback.invokeAfterLoadActions(session, entity, persister);
    }
    return entity;
}
Also used : CallbackImpl(org.hibernate.sql.exec.internal.CallbackImpl) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) QueryOptions(org.hibernate.query.spi.QueryOptions) SimpleQueryOptions(org.hibernate.query.internal.SimpleQueryOptions) Callback(org.hibernate.sql.exec.spi.Callback) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) SimpleQueryOptions(org.hibernate.query.internal.SimpleQueryOptions) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 2 with SimpleQueryOptions

use of org.hibernate.query.internal.SimpleQueryOptions in project hibernate-orm by hibernate.

the class AbstractNaturalIdLoader method selectByNaturalId.

/**
 * Perform a select, restricted by natural-id, based on `domainResultProducer` and `fetchProcessor`
 */
protected <L> L selectByNaturalId(Object bindValue, NaturalIdLoadOptions options, BiFunction<TableGroup, LoaderSqlAstCreationState, DomainResult<?>> domainResultProducer, LoaderSqlAstCreationState.FetchProcessor fetchProcessor, Function<Boolean, Long> statementStartHandler, BiConsumer<Object, Long> statementCompletionHandler, SharedSessionContractImplementor session) {
    final SessionFactoryImplementor sessionFactory = session.getFactory();
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
    final LockOptions lockOptions;
    if (options.getLockOptions() != null) {
        lockOptions = options.getLockOptions();
    } else {
        lockOptions = LockOptions.NONE;
    }
    final NavigablePath entityPath = new NavigablePath(entityDescriptor.getRootPathName());
    final QuerySpec rootQuerySpec = new QuerySpec(true);
    final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), lockOptions, fetchProcessor, true, sessionFactory);
    final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(true, entityPath, null, () -> rootQuerySpec::applyPredicate, sqlAstCreationState, sessionFactory);
    rootQuerySpec.getFromClause().addRoot(rootTableGroup);
    sqlAstCreationState.getFromClauseAccess().registerTableGroup(entityPath, rootTableGroup);
    final DomainResult<?> domainResult = domainResultProducer.apply(rootTableGroup, sqlAstCreationState);
    final SelectStatement sqlSelect = new SelectStatement(rootQuerySpec, Collections.singletonList(domainResult));
    final List<JdbcParameter> jdbcParameters = new ArrayList<>(naturalIdMapping.getJdbcTypeCount());
    final JdbcParameterBindings jdbcParamBindings = new JdbcParameterBindingsImpl(jdbcParameters.size());
    applyNaturalIdRestriction(bindValue, rootTableGroup, rootQuerySpec::applyPredicate, (jdbcParameter, jdbcParameterBinding) -> {
        jdbcParameters.add(jdbcParameter);
        jdbcParamBindings.addBinding(jdbcParameter, jdbcParameterBinding);
    }, sqlAstCreationState, session);
    final QueryOptions queryOptions = new SimpleQueryOptions(lockOptions, false);
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlSelect).translate(jdbcParamBindings, queryOptions);
    final StatisticsImplementor statistics = sessionFactory.getStatistics();
    final Long startToken = statementStartHandler.apply(statistics.isStatisticsEnabled());
    // noinspection unchecked
    final List<L> results = session.getFactory().getJdbcServices().getJdbcSelectExecutor().list(jdbcSelect, jdbcParamBindings, new ExecutionContext() {

        private final Callback callback = new CallbackImpl();

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

        @Override
        public QueryOptions getQueryOptions() {
            return queryOptions;
        }

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

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

        @Override
        public Callback getCallback() {
            return callback;
        }
    }, row -> (L) row[0], ListResultsConsumer.UniqueSemantic.FILTER);
    if (results.size() > 1) {
        throw new HibernateException(String.format("Loading by natural-id returned more that one row : %s", entityDescriptor.getEntityName()));
    }
    final L result;
    if (results.isEmpty()) {
        result = null;
    } else {
        result = results.get(0);
    }
    statementCompletionHandler.accept(result, startToken);
    return result;
}
Also used : LockOptions(org.hibernate.LockOptions) NavigablePath(org.hibernate.query.spi.NavigablePath) CallbackImpl(org.hibernate.sql.exec.internal.CallbackImpl) ArrayList(java.util.ArrayList) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) SimpleQueryOptions(org.hibernate.query.internal.SimpleQueryOptions) QueryOptions(org.hibernate.query.spi.QueryOptions) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) SimpleQueryOptions(org.hibernate.query.internal.SimpleQueryOptions) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) 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) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Aggregations

SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 SimpleQueryOptions (org.hibernate.query.internal.SimpleQueryOptions)2 QueryOptions (org.hibernate.query.spi.QueryOptions)2 QueryParameterBindings (org.hibernate.query.spi.QueryParameterBindings)2 CallbackImpl (org.hibernate.sql.exec.internal.CallbackImpl)2 JdbcParameterBindingsImpl (org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl)2 Callback (org.hibernate.sql.exec.spi.Callback)2 ExecutionContext (org.hibernate.sql.exec.spi.ExecutionContext)2 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)2 ArrayList (java.util.ArrayList)1 HibernateException (org.hibernate.HibernateException)1 LockOptions (org.hibernate.LockOptions)1 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 SqlAstTranslatorFactory (org.hibernate.sql.ast.SqlAstTranslatorFactory)1 SimpleFromClauseAccessImpl (org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl)1 SqlAliasBaseManager (org.hibernate.sql.ast.spi.SqlAliasBaseManager)1 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)1