Skip to content

Commit 23f9fac

Browse files
committed
HHH-19716 Set the collection owner when wrapping a collection into a persistent one
1 parent 9a84f8d commit 23f9fac

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ protected void firePostRemove(PersistentCollection<?> collection, Object id, Str
677677

678678
// collections ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
679679

680-
// Hibernate Reactive overrides this
680+
// Hibernate Reactive calls this
681681
protected void forEachOwnedCollection(
682682
Object entity, Object key,
683683
EntityPersister persister, BiConsumer<CollectionPersister, PersistentCollection<?>> action) {
@@ -698,7 +698,7 @@ protected void forEachOwnedCollection(
698698
collection =
699699
value == null
700700
? instantiateEmpty( key, descriptor )
701-
: wrap( descriptor, value );
701+
: wrap( descriptor, value, entity );
702702
}
703703
action.accept( descriptor, collection );
704704
}
@@ -712,12 +712,12 @@ protected PersistentCollection<?> instantiateEmpty(Object key, CollectionPersist
712712
return descriptor.getCollectionSemantics().instantiateWrapper(key, descriptor, this);
713713
}
714714

715-
//TODO: is this the right way to do this?
716-
// Hibernate Reactive calls this
717715
@SuppressWarnings({"rawtypes", "unchecked"})
718-
protected PersistentCollection<?> wrap(CollectionPersister descriptor, Object collection) {
716+
protected PersistentCollection<?> wrap(CollectionPersister descriptor, Object collection, Object owner) {
719717
final CollectionSemantics collectionSemantics = descriptor.getCollectionSemantics();
720-
return collectionSemantics.wrap(collection, descriptor, this);
718+
var wrapped = collectionSemantics.wrap( collection, descriptor, this );
719+
wrapped.setOwner( owner );
720+
return wrapped;
721721
}
722722

723723
// loading ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

hibernate-core/src/test/java/org/hibernate/orm/test/stateless/events/CollectionListenerInStatelessSessionTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.hibernate.testing.orm.junit.SessionFactoryScope;
1919
import org.junit.jupiter.api.Test;
2020

21+
import static org.assertj.core.api.Assertions.assertThat;
2122
import static org.junit.jupiter.api.Assertions.assertEquals;
2223

2324
@DomainModel(annotatedClasses = {EntityA.class, EntityB.class})
@@ -64,6 +65,8 @@ class MyPreCollectionRecreateEventListener implements PreCollectionRecreateEvent
6465

6566
@Override
6667
public void onPreRecreateCollection(PreCollectionRecreateEvent event) {
68+
assertThat( event.getAffectedOwnerOrNull() ).isNotNull();
69+
assertThat( event.getCollection().getOwner() ).isNotNull();
6770
called++;
6871
}
6972

@@ -75,6 +78,8 @@ class MyPreCollectionRemoveEventListener implements PreCollectionRemoveEventList
7578

7679
@Override
7780
public void onPreRemoveCollection(PreCollectionRemoveEvent event) {
81+
assertThat( event.getAffectedOwnerOrNull() ).isNotNull();
82+
assertThat( event.getCollection().getOwner() ).isNotNull();
7883
called++;
7984
}
8085

@@ -86,6 +91,8 @@ class MyPostCollectionRecreateEventListener implements PostCollectionRecreateEve
8691

8792
@Override
8893
public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
94+
assertThat( event.getAffectedOwnerOrNull() ).isNotNull();
95+
assertThat( event.getCollection().getOwner() ).isNotNull();
8996
called++;
9097
}
9198

@@ -97,6 +104,8 @@ class MyPostCollectionRemoveEventListener implements PostCollectionRemoveEventLi
97104

98105
@Override
99106
public void onPostRemoveCollection(PostCollectionRemoveEvent event) {
107+
assertThat( event.getAffectedOwnerOrNull() ).isNotNull();
108+
assertThat( event.getCollection().getOwner() ).isNotNull();
100109
called++;
101110
}
102111

0 commit comments

Comments
 (0)