Skip to content

Commit 4170c9e

Browse files
committed
HHH-19080 Allow Hibernate Reactive to initialize the non lazy collections via PersistenceContext
Hibernate Reactive needs a reactive version of the PersistenceContext to load non-lazy collections. But, now that `SatefulPersistenceContext` is package-private, Hibernate Reactive cannot extend the class anymore. These change make it possible to access the internal snapshots, so that the reactive implementation can implement the interface and doesn't need to extends `SatefulPersistenceContext`.
1 parent 91cdac4 commit 4170c9e

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,7 @@ public Object[] getDatabaseSnapshot(Object id, EntityPersister persister) throws
330330
}
331331
else {
332332
final Object[] snapshot = persister.getDatabaseSnapshot( id, session );
333-
if ( entitySnapshotsByKey == null ) {
334-
entitySnapshotsByKey = CollectionHelper.mapOfSize( INIT_COLL_SIZE );
335-
}
336-
entitySnapshotsByKey.put( key, snapshot == null ? NO_ROW : snapshot );
333+
getOrInitializeEntitySnapshotsByKey().put( key, snapshot == null ? NO_ROW : snapshot );
337334
return snapshot;
338335
}
339336
}
@@ -1187,7 +1184,8 @@ public void initializeNonLazyCollections() throws HibernateException {
11871184
initializeNonLazyCollections( PersistentCollection::forceInitialization );
11881185
}
11891186

1190-
protected void initializeNonLazyCollections(Consumer<PersistentCollection<?>> initializeAction ) {
1187+
@Override
1188+
public void initializeNonLazyCollections(Consumer<PersistentCollection<?>> initializeAction ) {
11911189
if ( loadCounter == 0 ) {
11921190
LOG.trace( "Initializing non-lazy collections" );
11931191

@@ -1314,6 +1312,21 @@ public Map<EntityKey,Object> getEntitiesByKey() {
13141312
return result;
13151313
}
13161314

1315+
// Used by Hibernate Reactive
1316+
@Override
1317+
public Map<EntityKey, Object> getEntitySnapshotsByKey() {
1318+
return entitySnapshotsByKey;
1319+
}
1320+
1321+
// Used by Hibernate Reactive
1322+
@Override
1323+
public Map<EntityKey, Object> getOrInitializeEntitySnapshotsByKey() {
1324+
if ( entitySnapshotsByKey == null ) {
1325+
entitySnapshotsByKey = CollectionHelper.mapOfSize( INIT_COLL_SIZE );
1326+
}
1327+
return entitySnapshotsByKey;
1328+
}
1329+
13171330
@Override
13181331
public Map<EntityKey, EntityHolder> getEntityHoldersByKey() {
13191332
//noinspection unchecked,rawtypes

hibernate-core/src/main/java/org/hibernate/engine/spi/PersistenceContext.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,18 @@ CollectionEntry addInitializedCollection(
444444
*/
445445
void initializeNonLazyCollections() throws HibernateException;
446446

447+
/**
448+
* Force initialization of all non-lazy collections encountered during
449+
* the current two-phase load (actually, this is a no-op, unless this
450+
* is the "outermost" load) allowing to customize how the initialization
451+
* should occur
452+
*
453+
* @see #initializeNonLazyCollections()
454+
* @param initializeAction the function that initialize the collection
455+
*/
456+
// Used by Hibernate Reactive
457+
void initializeNonLazyCollections(Consumer<PersistentCollection<?>> initializeAction);
458+
447459
/**
448460
* Get the {@code PersistentCollection} object for an array
449461
*/
@@ -533,6 +545,14 @@ EntityHolder claimEntityHolderIfPossible(
533545
@Internal
534546
Map<EntityKey,Object> getEntitiesByKey();
535547

548+
// Used by Hibernate Reactive
549+
@Internal
550+
Map<EntityKey,Object> getEntitySnapshotsByKey();
551+
552+
// Used by Hibernate Reactive
553+
@Internal
554+
Map<EntityKey,Object> getOrInitializeEntitySnapshotsByKey();
555+
536556
/**
537557
* Doubly internal
538558
*/

0 commit comments

Comments
 (0)