Search in sources :

Example 6 with LoadingEntityEntry

use of org.hibernate.sql.results.graph.entity.LoadingEntityEntry 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 7 with LoadingEntityEntry

use of org.hibernate.sql.results.graph.entity.LoadingEntityEntry 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 8 with LoadingEntityEntry

use of org.hibernate.sql.results.graph.entity.LoadingEntityEntry in project hibernate-orm by hibernate.

the class EntityDelayedFetchInitializer method resolveInstance.

@Override
public void resolveInstance(RowProcessingState rowProcessingState) {
    if (entityInstance != null) {
        return;
    }
    if (!isAttributeAssignableToConcreteDescriptor()) {
        return;
    }
    identifier = identifierAssembler.assemble(rowProcessingState);
    if (identifier == null) {
        // todo (6.0) : check this is the correct behaviour
        entityInstance = null;
    } else {
        final SharedSessionContractImplementor session = rowProcessingState.getSession();
        final EntityPersister concreteDescriptor = referencedModelPart.getEntityMappingType().getEntityPersister();
        if (!selectByUniqueKey) {
            final EntityKey entityKey = new EntityKey(identifier, concreteDescriptor);
            final PersistenceContext persistenceContext = session.getPersistenceContext();
            final LoadingEntityEntry loadingEntityLocally = persistenceContext.getLoadContexts().findLoadingEntityEntry(entityKey);
            if (loadingEntityLocally != null) {
                entityInstance = loadingEntityLocally.getEntityInstance();
            }
        }
        if (entityInstance == null) {
            if (referencedModelPart.isOptional() && parentAccess != null && parentAccess.getInitializedPart().findContainingEntityMapping().getEntityPersister().getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()) {
                entityInstance = LazyPropertyInitializer.UNFETCHED_PROPERTY;
            } else {
                if (selectByUniqueKey) {
                    final String uniqueKeyPropertyName = referencedModelPart.getBidirectionalAttributeName();
                    final Type uniqueKeyPropertyType = (referencedModelPart.getReferencedPropertyName() == null) ? concreteDescriptor.getIdentifierType() : session.getFactory().getReferencedPropertyType(concreteDescriptor.getEntityName(), uniqueKeyPropertyName);
                    final EntityUniqueKey euk = new EntityUniqueKey(concreteDescriptor.getEntityName(), uniqueKeyPropertyName, identifier, uniqueKeyPropertyType, session.getFactory());
                    final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
                    entityInstance = persistenceContext.getEntity(euk);
                    if (entityInstance == null) {
                        if (((AbstractEntityInitializer) this.parentAccess).getEntityDescriptor().getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()) {
                            return;
                        }
                        entityInstance = ((UniqueKeyLoadable) concreteDescriptor).loadByUniqueKey(uniqueKeyPropertyName, identifier, session);
                        // add it to the Persistence Context
                        if (entityInstance != null) {
                            persistenceContext.addEntity(euk, entityInstance);
                        }
                    }
                    if (entityInstance != null) {
                        entityInstance = persistenceContext.proxyFor(entityInstance);
                    }
                } else {
                    entityInstance = session.internalLoad(concreteDescriptor.getEntityName(), identifier, false, false);
                }
                if (entityInstance instanceof HibernateProxy) {
                    ((HibernateProxy) entityInstance).getHibernateLazyInitializer().setUnwrap(referencedModelPart.isUnwrapProxy() && concreteDescriptor.isInstrumented());
                }
            }
        }
        notifyResolutionListeners(entityInstance);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) Type(org.hibernate.type.Type) EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) AbstractEntityInitializer(org.hibernate.sql.results.graph.entity.AbstractEntityInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry)

Aggregations

LoadingEntityEntry (org.hibernate.sql.results.graph.entity.LoadingEntityEntry)8 EntityKey (org.hibernate.engine.spi.EntityKey)7 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)6 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)4 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)4 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)4 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)4 QueryOptions (org.hibernate.query.spi.QueryOptions)4 QueryParameterBindings (org.hibernate.query.spi.QueryParameterBindings)4 SqlAstTranslatorFactory (org.hibernate.sql.ast.SqlAstTranslatorFactory)4 Callback (org.hibernate.sql.exec.spi.Callback)4 ExecutionContext (org.hibernate.sql.exec.spi.ExecutionContext)4 JdbcSelect (org.hibernate.sql.exec.spi.JdbcSelect)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 JdbcParameterBindingsImpl (org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl)3 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)3 CollectionKey (org.hibernate.engine.spi.CollectionKey)2 HibernateProxy (org.hibernate.proxy.HibernateProxy)2 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)2 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)2