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);
}
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);
}
}
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);
}
}
Aggregations