Search in sources :

Example 1 with QueryParameterBindings

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

the class NativeNonSelectQueryPlanImpl method executeUpdate.

@Override
public int executeUpdate(DomainQueryExecutionContext executionContext) {
    executionContext.getSession().autoFlushIfRequired(affectedTableNames);
    BulkOperationCleanupAction.schedule(executionContext.getSession(), affectedTableNames);
    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 JdbcMutation jdbcMutation = new NativeJdbcMutation(sql, jdbcParameterBinders, affectedTableNames);
    final JdbcMutationExecutor executor = StandardJdbcMutationExecutor.INSTANCE;
    final SharedSessionContractImplementor session = executionContext.getSession();
    // return jdbcServices.getJdbcMutationExecutor().execute(
    return executor.execute(jdbcMutation, jdbcParameterBindings, sql -> session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql), (integer, preparedStatement) -> {
    }, SqmJdbcExecutionContextAdapter.usingLockingAndPaging(executionContext));
}
Also used : NativeJdbcMutation(org.hibernate.sql.exec.spi.NativeJdbcMutation) NativeJdbcMutation(org.hibernate.sql.exec.spi.NativeJdbcMutation) JdbcMutation(org.hibernate.sql.exec.spi.JdbcMutation) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBinder(org.hibernate.sql.exec.spi.JdbcParameterBinder) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings) JdbcMutationExecutor(org.hibernate.sql.exec.spi.JdbcMutationExecutor) StandardJdbcMutationExecutor(org.hibernate.sql.exec.internal.StandardJdbcMutationExecutor)

Example 2 with QueryParameterBindings

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

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

the class SingleUniqueKeyEntityLoaderStandard method resolveId.

@Override
public Object resolveId(Object ukValue, 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.singletonList(entityDescriptor.getIdentifierMapping()), 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() {

        @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 UnsupportedOperationException("Follow-on locking not supported yet");
        }
    }, row -> row[0], ListResultsConsumer.UniqueSemantic.FILTER);
    assert list.size() == 1;
    return list.get(0);
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) ArrayList(java.util.ArrayList) 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) 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 4 with QueryParameterBindings

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

the class CollectionElementLoaderByIndex method load.

public Object load(Object key, Object index, SharedSessionContractImplementor session) {
    final SessionFactoryImplementor sessionFactory = session.getFactory();
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
    final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(keyJdbcCount);
    int offset = jdbcParameterBindings.registerParametersForEachJdbcValue(key, Clause.WHERE, attributeMapping.getKeyDescriptor(), jdbcParameters, session);
    offset += jdbcParameterBindings.registerParametersForEachJdbcValue(incrementIndexByBase(index), Clause.WHERE, offset, attributeMapping.getIndexDescriptor(), jdbcParameters, session);
    assert offset == jdbcParameters.size();
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(jdbcParameterBindings, QueryOptions.NONE);
    List<Object> list = jdbcServices.getJdbcSelectExecutor().list(jdbcSelect, jdbcParameterBindings, new ExecutionContext() {

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

        @Override
        public CollectionKey getCollectionKey() {
            return null;
        }

        @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() {
            return null;
        }
    }, RowTransformerPassThruImpl.instance(), ListResultsConsumer.UniqueSemantic.FILTER);
    if (list.isEmpty()) {
        return null;
    }
    return list.get(0);
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) CollectionKey(org.hibernate.engine.spi.CollectionKey) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) QueryOptions(org.hibernate.query.spi.QueryOptions) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) 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 5 with QueryParameterBindings

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

the class CollectionLoaderSubSelectFetch method load.

@Override
public PersistentCollection<?> load(Object triggerKey, SharedSessionContractImplementor session) {
    final CollectionKey collectionKey = new CollectionKey(attributeMapping.getCollectionDescriptor(), triggerKey);
    final SessionFactoryImplementor sessionFactory = session.getFactory();
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    // try to find a registered SubselectFetch
    final PersistentCollection<?> collection = persistenceContext.getCollection(collectionKey);
    attributeMapping.getCollectionDescriptor().getCollectionType().getKeyOfOwner(collection.getOwner(), session);
    final EntityEntry ownerEntry = persistenceContext.getEntry(collection.getOwner());
    final BatchFetchQueue batchFetchQueue = persistenceContext.getBatchFetchQueue();
    final EntityKey triggerKeyOwnerKey = ownerEntry.getEntityKey();
    final SubselectFetch registeredFetch = batchFetchQueue.getSubselect(triggerKeyOwnerKey);
    List<PersistentCollection<?>> subSelectFetchedCollections = null;
    if (registeredFetch != null) {
        subSelectFetchedCollections = CollectionHelper.arrayList(registeredFetch.getResultingEntityKeys().size());
        // there was one, so we want to make sure to prepare the corresponding collection
        // reference for reading
        final Iterator<EntityKey> itr = registeredFetch.getResultingEntityKeys().iterator();
        while (itr.hasNext()) {
            final EntityKey key = itr.next();
            final PersistentCollection<?> containedCollection = persistenceContext.getCollection(new CollectionKey(attributeMapping.getCollectionDescriptor(), key.getIdentifier()));
            if (containedCollection != collection) {
                containedCollection.beginRead();
                containedCollection.beforeInitialize(getLoadable().getCollectionDescriptor(), -1);
                subSelectFetchedCollections.add(containedCollection);
            }
        }
    }
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(this.subselect.getLoadingJdbcParameterBindings(), QueryOptions.NONE);
    final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(batchFetchQueue, sqlAst, this.subselect.getLoadingJdbcParameters(), this.subselect.getLoadingJdbcParameterBindings());
    jdbcServices.getJdbcSelectExecutor().list(jdbcSelect, this.subselect.getLoadingJdbcParameterBindings(), 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);
    if (subSelectFetchedCollections != null && !subSelectFetchedCollections.isEmpty()) {
        subSelectFetchedCollections.forEach(c -> {
            if (c.wasInitialized()) {
                return;
            }
            c.initializeEmptyCollection(getLoadable().getCollectionDescriptor());
            ResultsHelper.finalizeCollectionLoading(persistenceContext, getLoadable().getCollectionDescriptor(), c, c.getKey(), true);
        });
        subSelectFetchedCollections.clear();
    }
    return collection;
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CollectionKey(org.hibernate.engine.spi.CollectionKey) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) QueryOptions(org.hibernate.query.spi.QueryOptions) BatchFetchQueue(org.hibernate.engine.spi.BatchFetchQueue) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) EntityKey(org.hibernate.engine.spi.EntityKey) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry) EntityEntry(org.hibernate.engine.spi.EntityEntry) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry)

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