-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-19115 - implement ordered multiloading with natural ids #9783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jrenaat
merged 1 commit into
hibernate:main
from
jrenaat:HHH-19115_orderedMultiNaturalIdLoad
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
...te-core/src/main/java/org/hibernate/loader/ast/internal/AbstractMultiNaturalIdLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.loader.ast.internal; | ||
|
|
||
|
|
||
| import org.hibernate.LockOptions; | ||
| import org.hibernate.engine.spi.EntityEntry; | ||
| import org.hibernate.engine.spi.EntityKey; | ||
| import org.hibernate.engine.spi.PersistenceContext; | ||
| import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
| import org.hibernate.loader.ast.spi.MultiNaturalIdLoadOptions; | ||
| import org.hibernate.loader.ast.spi.MultiNaturalIdLoader; | ||
| import org.hibernate.metamodel.mapping.EntityMappingType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * @author Jan Schatteman | ||
| */ | ||
| public abstract class AbstractMultiNaturalIdLoader<E> implements MultiNaturalIdLoader<E> { | ||
| private final EntityMappingType entityDescriptor; | ||
|
|
||
| protected MultiNaturalIdLoadOptions options; | ||
|
|
||
| public AbstractMultiNaturalIdLoader(EntityMappingType entityDescriptor) { | ||
| this.entityDescriptor = entityDescriptor; | ||
| } | ||
|
|
||
| @Override | ||
| public <K> List<E> multiLoad(K[] naturalIds, MultiNaturalIdLoadOptions options, SharedSessionContractImplementor session) { | ||
| assert naturalIds != null; | ||
|
|
||
| this.options = options; | ||
|
|
||
| if ( naturalIds.length == 0 ) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| return options.isOrderReturnEnabled() | ||
| ? performOrderedMultiLoad( naturalIds, options, session ) | ||
| : performUnorderedMultiLoad( naturalIds, options, session ); | ||
| } | ||
|
|
||
| private <K> List<E> performUnorderedMultiLoad(K[] naturalIds, MultiNaturalIdLoadOptions options, SharedSessionContractImplementor session) { | ||
| if ( MultiKeyLoadLogging.MULTI_KEY_LOAD_LOGGER.isTraceEnabled() ) { | ||
| MultiKeyLoadLogging.MULTI_KEY_LOAD_LOGGER.tracef( "Unordered MultiLoad Starting - `%s`", getEntityDescriptor().getEntityName() ); | ||
| } | ||
|
|
||
| return unorderedMultiLoad( | ||
| naturalIds, | ||
| session, | ||
| options.getLockOptions() == null ? LockOptions.NONE : options.getLockOptions() | ||
| ); | ||
| } | ||
|
|
||
| protected abstract <K> List<E> unorderedMultiLoad(K[] naturalIds, SharedSessionContractImplementor session, LockOptions lockOptions); | ||
|
|
||
| private <K> List<E> performOrderedMultiLoad(K[] naturalIds, MultiNaturalIdLoadOptions options, SharedSessionContractImplementor session) { | ||
| if ( MultiKeyLoadLogging.MULTI_KEY_LOAD_LOGGER.isTraceEnabled() ) { | ||
| MultiKeyLoadLogging.MULTI_KEY_LOAD_LOGGER.tracef( "Ordered MultiLoad Starting - `%s`", getEntityDescriptor().getEntityName() ); | ||
| } | ||
|
|
||
| return orderedMultiLoad( | ||
| naturalIds, | ||
| session, | ||
| options.getLockOptions() == null ? LockOptions.NONE : options.getLockOptions() | ||
| ); | ||
| } | ||
|
|
||
| protected <K> List<E> orderedMultiLoad( K[] naturalIds, SharedSessionContractImplementor session, LockOptions lockOptions ) { | ||
|
|
||
| unorderedMultiLoad( naturalIds, session, lockOptions ); | ||
|
|
||
| return handleResults( naturalIds, session, lockOptions ); | ||
| } | ||
|
|
||
| protected <K> List<E> handleResults( K[] naturalIds, SharedSessionContractImplementor session, LockOptions lockOptions ) { | ||
| List<E> results = new ArrayList<>(naturalIds.length); | ||
| for ( int i = 0; i < naturalIds.length; i++ ) { | ||
| final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); | ||
|
|
||
| Object id = persistenceContext.getNaturalIdResolutions().findCachedIdByNaturalId( naturalIds[i], getEntityDescriptor() ); | ||
|
|
||
| // Id can be null if a non-existent natural id is requested | ||
| Object entity = id == null ? null | ||
| : persistenceContext.getEntity( new EntityKey( id, getEntityDescriptor().getEntityPersister() ) ); | ||
| if ( entity != null && !options.isReturnOfDeletedEntitiesEnabled() ) { | ||
| // make sure it is not DELETED | ||
| final EntityEntry entry = persistenceContext.getEntry( entity ); | ||
| if ( entry.getStatus().isDeletedOrGone() ) { | ||
| // the entity is locally deleted, and the options ask that we not return such entities... | ||
| entity = null; | ||
| } | ||
| else { | ||
| entity = persistenceContext.proxyFor( entity ); | ||
| } | ||
| } | ||
| results.add( (E) entity ); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| @Override | ||
| public EntityMappingType getLoadable() { | ||
| return getEntityDescriptor(); | ||
| } | ||
|
|
||
| protected EntityMappingType getEntityDescriptor() { | ||
| return entityDescriptor; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Useless parameter Note