Skip to content

Commit

Permalink
HHH-18719 Previous row state reuse can provide detaches entities to t…
Browse files Browse the repository at this point in the history
…he consumer
  • Loading branch information
dreab8 committed Oct 22, 2024
1 parent 96c61c3 commit 356b7f5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,14 @@ public void clear() {
if ( entitiesByKey != null ) {
//Strictly avoid lambdas in this case
for ( EntityHolderImpl value : entitiesByKey.values() ) {
if ( value != null && value.proxy != null ) {
extractLazyInitializer( value.proxy ).unsetSession();
if ( value != null ) {
value.state = EntityHolderState.DETACHED;
if ( value.proxy != null ) {
final LazyInitializer lazyInitializer = extractLazyInitializer( value.proxy );
if ( lazyInitializer != null ) {
lazyInitializer.unsetSession();
}
}
}
}
}
Expand Down Expand Up @@ -2238,6 +2244,11 @@ public boolean isEventuallyInitialized() {
return state == EntityHolderState.INITIALIZED || entityInitializer != null;
}

@Override
public boolean isDetached() {
return state == EntityHolderState.DETACHED;
}

public static EntityHolderImpl forProxy(EntityKey entityKey, EntityPersister descriptor, Object proxy) {
return new EntityHolderImpl( entityKey, descriptor, null, proxy );
}
Expand All @@ -2250,7 +2261,8 @@ public static EntityHolderImpl forEntity(EntityKey entityKey, EntityPersister de
enum EntityHolderState {
UNINITIALIZED,
ENHANCED_PROXY,
INITIALIZED
INITIALIZED,
DETACHED
}

// NATURAL ID RESOLUTION HANDLING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -2266,4 +2278,13 @@ public NaturalIdResolutions getNaturalIdResolutions() {
return naturalIdResolutions;
}

@Override
public EntityHolder detachEntity(EntityKey key) {
final EntityHolderImpl entityHolder = removeEntityHolder( key );
if ( entityHolder != null ) {
entityHolder.state = EntityHolderState.DETACHED;
}
return entityHolder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public interface EntityHolder {
* Whether the entity is already initialized or will be initialized through an initializer eventually.
*/
boolean isEventuallyInitialized();

/**
* Whether the entity is detached.
*/
boolean isDetached();
}
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,12 @@ EntityHolder claimEntityHolderIfPossible(
* @return This persistence context's natural-id helper
*/
NaturalIdResolutions getNaturalIdResolutions();

/**
Remove the {@link EntityHolder} and set its state to DETACHED
*/
default @Nullable EntityHolder detachEntity(EntityKey key) {
EntityHolder entityHolder = removeEntityHolder( key );
return entityHolder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onEvict(EvictEvent event) throws HibernateException {
.getMappingMetamodel()
.getEntityDescriptor( lazyInitializer.getEntityName() );
final EntityKey key = source.generateEntityKey( id, persister );
final EntityHolder holder = persistenceContext.removeEntityHolder( key );
final EntityHolder holder = persistenceContext.detachEntity( key );
// if the entity has been evicted then its holder is null
if ( holder != null && !lazyInitializer.isUninitialized() ) {
final Object entity = holder.getEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ protected void resolveKey(EntityInitializerData data, boolean entityKeyOnly) {
}

if ( oldEntityKey != null && previousRowReuse && oldEntityInstance != null
&& areKeysEqual( oldEntityKey.getIdentifier(), id ) ) {
&& areKeysEqual( oldEntityKey.getIdentifier(), id ) && !oldEntityHolder.isDetached() ) {
data.setState( State.INITIALIZED );
data.entityKey = oldEntityKey;
data.setInstance( oldEntityInstance );
Expand Down

0 comments on commit 356b7f5

Please sign in to comment.