Skip to content

Commit bf2602d

Browse files
committed
HHH-19716 Set the collection owner when wrapping a collection into a persistent one
1 parent 22e4e62 commit bf2602d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ protected void forEachOwnedCollection(
712712
collection =
713713
value == null
714714
? instantiateEmpty( key, descriptor )
715-
: wrap( descriptor, value );
715+
: wrap( descriptor, value, entity );
716716
}
717717
action.accept( descriptor, collection );
718718
}
@@ -729,9 +729,11 @@ protected PersistentCollection<?> instantiateEmpty(Object key, CollectionPersist
729729
//TODO: is this the right way to do this?
730730
// Hibernate Reactive calls this
731731
@SuppressWarnings({"rawtypes", "unchecked"})
732-
protected PersistentCollection<?> wrap(CollectionPersister descriptor, Object collection) {
732+
protected PersistentCollection<?> wrap(CollectionPersister descriptor, Object collection, Object owner) {
733733
final CollectionSemantics collectionSemantics = descriptor.getCollectionSemantics();
734-
return collectionSemantics.wrap(collection, descriptor, this);
734+
var wrapped = collectionSemantics.wrap( collection, descriptor, this );
735+
wrapped.setOwner( owner );
736+
return wrapped;
735737
}
736738

737739
// 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)