Search in sources :

Example 11 with JdbcSelect

use of org.hibernate.sql.exec.spi.JdbcSelect 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 12 with JdbcSelect

use of org.hibernate.sql.exec.spi.JdbcSelect 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)

Example 13 with JdbcSelect

use of org.hibernate.sql.exec.spi.JdbcSelect in project hibernate-orm by hibernate.

the class CollectionLoaderSingleKey method load.

@Override
public PersistentCollection<?> load(Object key, SharedSessionContractImplementor session) {
    final CollectionKey collectionKey = new CollectionKey(attributeMapping.getCollectionDescriptor(), key);
    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);
    assert offset == jdbcParameters.size();
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(jdbcParameterBindings, QueryOptions.NONE);
    final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(session.getPersistenceContext().getBatchFetchQueue(), sqlAst, jdbcParameters, jdbcParameterBindings);
    jdbcServices.getJdbcSelectExecutor().list(jdbcSelect, jdbcParameterBindings, new ExecutionContext() {

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

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

        @Override
        public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) {
            subSelectFetchableKeysHandler.addKey(entityKey, entry);
        }

        @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);
    return session.getPersistenceContext().getCollection(collectionKey);
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) 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) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) 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 14 with JdbcSelect

use of org.hibernate.sql.exec.spi.JdbcSelect in project hibernate-orm by hibernate.

the class MultiIdLoaderStandard method loadEntitiesById.

private List<T> loadEntitiesById(List<Object> idsInBatch, LockOptions lockOptions, SharedSessionContractImplementor session) {
    assert idsInBatch != null;
    assert !idsInBatch.isEmpty();
    final int numberOfIdsInBatch = idsInBatch.size();
    if (numberOfIdsInBatch == 1) {
        return performSingleMultiLoad(idsInBatch.get(0), lockOptions, session);
    }
    if (log.isTraceEnabled()) {
        log.tracef("#loadEntitiesById(`%s`, `%s`, ..)", entityDescriptor.getEntityName(), numberOfIdsInBatch);
    }
    final List<JdbcParameter> jdbcParameters = new ArrayList<>(numberOfIdsInBatch * idJdbcTypeCount);
    final SelectStatement sqlAst = LoaderSelectBuilder.createSelect(getLoadable(), // null here means to select everything
    null, getLoadable().getIdentifierMapping(), null, numberOfIdsInBatch, session.getLoadQueryInfluencers(), lockOptions, 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 = 0;
    for (int i = 0; i < numberOfIdsInBatch; i++) {
        final Object id = idsInBatch.get(i);
        offset += jdbcParameterBindings.registerParametersForEachJdbcValue(id, Clause.WHERE, offset, entityDescriptor.getIdentifierMapping(), jdbcParameters, session);
    }
    // we should have used all the JdbcParameter references (created bindings for all)
    assert offset == jdbcParameters.size();
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(jdbcParameterBindings, QueryOptions.NONE);
    final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler;
    if (entityDescriptor.hasSubselectLoadableCollections()) {
        subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(session.getPersistenceContext().getBatchFetchQueue(), sqlAst, jdbcParameters, jdbcParameterBindings);
    } else {
        subSelectFetchableKeysHandler = null;
    }
    return JdbcSelectExecutorStandardImpl.INSTANCE.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() {
            return null;
        }

        @Override
        public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) {
            if (subSelectFetchableKeysHandler != null) {
                subSelectFetchableKeysHandler.addKey(entityKey, entry);
            }
        }
    }, RowTransformerPassThruImpl.instance(), ListResultsConsumer.UniqueSemantic.FILTER);
}
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) 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) 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 15 with JdbcSelect

use of org.hibernate.sql.exec.spi.JdbcSelect in project hibernate-orm by hibernate.

the class AbstractNaturalIdLoader method resolveIdToNaturalId.

@Override
public Object resolveIdToNaturalId(Object id, SharedSessionContractImplementor session) {
    final SessionFactoryImplementor sessionFactory = session.getFactory();
    final List<JdbcParameter> jdbcParameters = new ArrayList<>();
    final SelectStatement sqlSelect = LoaderSelectBuilder.createSelect(entityDescriptor(), Collections.singletonList(naturalIdMapping()), entityDescriptor().getIdentifierMapping(), null, 1, session.getLoadQueryInfluencers(), LockOptions.NONE, jdbcParameters::add, sessionFactory);
    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, sqlSelect).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 UnsupportedOperationException("Follow-on locking not supported yet");
        }
    }, (row) -> {
        // because we select the natural-id we want to "reduce" the result
        assert row.length == 1;
        return row[0];
    }, ListResultsConsumer.UniqueSemantic.FILTER);
    if (results.isEmpty()) {
        return null;
    }
    if (results.size() > 1) {
        throw new HibernateException(String.format("Resolving id to natural-id returned more that one row : %s #%s", entityDescriptor().getEntityName(), id));
    }
    return results.get(0);
}
Also used : 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) 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) SimpleQueryOptions(org.hibernate.query.internal.SimpleQueryOptions) 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)

Aggregations

JdbcSelect (org.hibernate.sql.exec.spi.JdbcSelect)21 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)17 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)16 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)14 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)14 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)13 JdbcParameterBindingsImpl (org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl)13 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)12 QueryParameterBindings (org.hibernate.query.spi.QueryParameterBindings)12 SqlAstTranslatorFactory (org.hibernate.sql.ast.SqlAstTranslatorFactory)12 ArrayList (java.util.ArrayList)11 ExecutionContext (org.hibernate.sql.exec.spi.ExecutionContext)11 QueryOptions (org.hibernate.query.spi.QueryOptions)10 Callback (org.hibernate.sql.exec.spi.Callback)10 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)9 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)6 List (java.util.List)5 EntityKey (org.hibernate.engine.spi.EntityKey)5 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)5 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)5