Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public abstract class AbstractEntityBatchLoader<T>

private final SingleIdEntityLoaderStandardImpl<T> singleIdLoader;

public AbstractEntityBatchLoader(EntityMappingType entityDescriptor, LoadQueryInfluencers loadQueryInfluencers) {
super( entityDescriptor, loadQueryInfluencers.getSessionFactory() );
this.singleIdLoader = new SingleIdEntityLoaderStandardImpl<>( entityDescriptor, loadQueryInfluencers );
public AbstractEntityBatchLoader(EntityMappingType entityDescriptor, LoadQueryInfluencers influencers) {
super( entityDescriptor, influencers.getSessionFactory() );
this.singleIdLoader = new SingleIdEntityLoaderStandardImpl<>( entityDescriptor, influencers );
}

protected abstract void initializeEntities(
Expand All @@ -49,7 +49,6 @@ public final T load(
}

final Object[] ids = resolveIdsToInitialize( id, session );

return load( id, ids, hasSingleId( ids ), entityInstance, lockOptions, readOnly, session );
}

Expand All @@ -65,9 +64,7 @@ public T load(

final Object[] ids = resolveIdsToInitialize( id, session );
final boolean hasSingleId = hasSingleId( ids );

final T entity = load( id, ids, hasSingleId, entityInstance, lockOptions, null, session );

if ( hasSingleId ) {
return entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private List<T> orderedMultiLoad(

final int maxBatchSize = maxBatchSize( ids, loadOptions );

final List<Object> result = arrayList( ids.length );
final List<Object> results = arrayList( ids.length );

final List<Object> idsInBatch = new ArrayList<>();
final List<Integer> elementPositionsLoadedByBatch = new ArrayList<>();
Expand All @@ -131,7 +131,7 @@ private List<T> orderedMultiLoad(
final Object id = idCoercionEnabled ? idType.coerce( ids[i], session ) : ids[i];
final EntityKey entityKey = new EntityKey( id, getLoadable().getEntityPersister() );

if ( !loadFromEnabledCaches( loadOptions, session, id, lockOptions, entityKey, result, i ) ) {
if ( !loadFromEnabledCaches( loadOptions, session, id, lockOptions, entityKey, results, i ) ) {
// if we did not hit any of the continues above,
// then we need to batch load the entity state.
idsInBatch.add( id );
Expand All @@ -143,7 +143,7 @@ private List<T> orderedMultiLoad(
}

// Save the EntityKey instance for use later
result.add( i, entityKey );
results.add( i, entityKey );
elementPositionsLoadedByBatch.add( i );
}
}
Expand All @@ -155,10 +155,10 @@ private List<T> orderedMultiLoad(
}

// for each result where we set the EntityKey earlier, replace them
handleResults( loadOptions, session, elementPositionsLoadedByBatch, result );
handleResults( loadOptions, session, elementPositionsLoadedByBatch, results );

//noinspection unchecked
return (List<T>) result;
return (List<T>) results;
}

private static LockOptions lockOptions(MultiIdLoadOptions loadOptions) {
Expand Down Expand Up @@ -255,12 +255,12 @@ protected List<T> unorderedMultiLoad(
MultiIdLoadOptions loadOptions,
SharedSessionContractImplementor session) {
final LockOptions lockOptions = lockOptions( loadOptions );
final List<T> result = arrayList( ids.length );
final List<T> results = arrayList( ids.length );
final Object[] unresolvableIds =
resolveInCachesIfEnabled( ids, loadOptions, lockOptions, session,
(position, entityKey, resolvedRef) -> result.add( (T) resolvedRef ) );
(position, entityKey, resolvedRef) -> results.add( (T) resolvedRef ) );
if ( !isEmpty( unresolvableIds ) ) {
loadEntitiesWithUnresolvedIds( unresolvableIds, loadOptions, lockOptions, result, session );
loadEntitiesWithUnresolvedIds( unresolvableIds, loadOptions, lockOptions, results, session );
final BatchFetchQueue batchFetchQueue = session.getPersistenceContextInternal().getBatchFetchQueue();
final EntityPersister persister = getLoadable().getEntityPersister();
for ( Object id : unresolvableIds ) {
Expand All @@ -272,7 +272,7 @@ protected List<T> unorderedMultiLoad(
}
}
}
return result;
return results;
}

protected abstract void loadEntitiesWithUnresolvedIds(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.hibernate.metamodel.mapping.NaturalIdMapping;

import java.util.List;
import java.util.function.Consumer;

import static java.util.Collections.emptyList;
import static org.hibernate.internal.util.collections.CollectionHelper.arrayList;
Expand Down Expand Up @@ -69,7 +70,7 @@ private <K> List<E> unorderedMultiLoad(
final List<E> results = arrayList( naturalIds.length );
final LockOptions lockOptions = lockOptions( loadOptions );
final Object[] unresolvedIds =
checkPersistenceContextForCachedResults( naturalIds, loadOptions, session, lockOptions, results );
checkPersistenceContextForCachedResults( naturalIds, loadOptions, session, lockOptions, results::add );
if ( !isEmpty( unresolvedIds ) ) {
results.addAll( loadEntitiesWithUnresolvedIds( unresolvedIds, loadOptions, lockOptions, session ) );
}
Expand All @@ -82,7 +83,10 @@ protected abstract List<E> loadEntitiesWithUnresolvedIds(
LockOptions lockOptions,
SharedSessionContractImplementor session);

private <K> List<E> performOrderedMultiLoad(K[] naturalIds, MultiNaturalIdLoadOptions options, SharedSessionContractImplementor session) {
private <K> List<E> performOrderedMultiLoad(
K[] naturalIds,
MultiNaturalIdLoadOptions options,
SharedSessionContractImplementor session) {
if ( MULTI_KEY_LOAD_LOGGER.isTraceEnabled() ) {
MULTI_KEY_LOAD_LOGGER.trace( "Ordered MultiLoad starting: "
+ getEntityDescriptor().getEntityName() );
Expand All @@ -94,7 +98,12 @@ private <K> List<E> orderedMultiLoad(
K[] naturalIds,
MultiNaturalIdLoadOptions loadOptions,
SharedSessionContractImplementor session) {
unorderedMultiLoad( naturalIds, loadOptions, session );
final LockOptions lockOptions = lockOptions( loadOptions );
final Object[] unresolvedIds =
checkPersistenceContextForCachedResults( naturalIds, loadOptions, session, lockOptions, result -> {} );
if ( !isEmpty( unresolvedIds ) ) {
loadEntitiesWithUnresolvedIds( unresolvedIds, loadOptions, lockOptions, session );
}
return sortResults( naturalIds, loadOptions, session );
}

Expand Down Expand Up @@ -133,7 +142,7 @@ private <K> Object[] checkPersistenceContextForCachedResults(
MultiNaturalIdLoadOptions loadOptions,
SharedSessionContractImplementor session,
LockOptions lockOptions,
List<E> results ) {
Consumer<E> results ) {
final List<K> unresolvedIds = arrayList( naturalIds.length );
final PersistenceContext context = session.getPersistenceContextInternal();
final NaturalIdMapping naturalIdMapping = getEntityDescriptor().getNaturalIdMapping();
Expand All @@ -147,7 +156,7 @@ private <K> Object[] checkPersistenceContextForCachedResults(
// either a managed entry, or a deleted one with returnDeleted enabled
upgradeLock( entity, entry, lockOptions, session );
final Object result = context.proxyFor( entity );
results.add( (E) result );
results.accept( (E) result );
}
}
else {
Expand All @@ -166,7 +175,7 @@ public EntityMappingType getLoadable() {
return getEntityDescriptor();
}

protected EntityMappingType getEntityDescriptor() {
protected final EntityMappingType getEntityDescriptor() {
return entityDescriptor;
}
}
Loading