Skip to content

Commit 3184820

Browse files
committed
HHH-2862, HHH-1914 Make collection replacement a first class operation on PersistenceContext
1 parent a920bf1 commit 3184820

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,23 @@ public void addNewCollection(CollectionPersister persister, PersistentCollection
10751075
addCollection( collection, persister );
10761076
}
10771077

1078+
@Override
1079+
public void replaceCollection(CollectionPersister persister, PersistentCollection<?> collection) {
1080+
assert !collection.isDirectlyAccessible();
1081+
final CollectionEntry entry = new CollectionEntry( collection, session.getFactory() );
1082+
final IdentityMap<PersistentCollection<?>, CollectionEntry> collectionEntries = getOrInitializeCollectionEntries();
1083+
collectionEntries.put( collection, entry );
1084+
final CollectionKey collectionKey = new CollectionKey( entry.getLoadedPersister(), collection.getKey() );
1085+
final PersistentCollection<?> old = addCollectionByKey( collectionKey, collection );
1086+
if ( old == null ) {
1087+
throw new HibernateException( "No collection for replacement found: " + collectionKey.getRole() );
1088+
}
1089+
if ( !old.isDirectlyAccessible() ) {
1090+
throw new HibernateException( "Replacement of not directly accessible collection found: " + collectionKey.getRole() );
1091+
}
1092+
collectionEntries.remove( old );
1093+
}
1094+
10781095
/**
10791096
* Add a collection to the cache, with a given collection entry.
10801097
*

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.checkerframework.checker.nullness.qual.Nullable;
2727

28+
2829
/**
2930
* Represents the state of "stuff" Hibernate is tracking, including (not exhaustive):
3031
* <ul>
@@ -408,6 +409,13 @@ void addInitializedDetachedCollection(
408409
CollectionPersister collectionPersister,
409410
PersistentCollection<?> collection);
410411

412+
/**
413+
* Replaces a directly accessible collection with the given one
414+
* @param collection The collection to be associated with the persistence context
415+
* @since 7.0
416+
*/
417+
void replaceCollection(CollectionPersister persister, PersistentCollection<?> collection);
418+
411419
/**
412420
* add a collection we just pulled out of the cache (does not need initializing)
413421
*/

hibernate-core/src/main/java/org/hibernate/type/CollectionType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ private Object replaceOriginal(
671671
final PersistentCollection<?> persistentCollection = instantiate( session, collectionPersister, key );
672672
persistentCollection.initializeEmptyCollection( collectionPersister );
673673
persistentCollection.setSnapshot( key, existingPersistentCollection.getRole(), existingPersistentCollection.getStoredSnapshot() );
674-
session.getPersistenceContextInternal().addInitializedDetachedCollection( collectionPersister, persistentCollection );
674+
session.getPersistenceContextInternal().replaceCollection( collectionPersister, persistentCollection );
675675
target = persistentCollection;
676676
}
677677
//TODO: this is a little inefficient, don't need to do a whole

0 commit comments

Comments
 (0)