Skip to content

Commit 14956fd

Browse files
committed
HHH-6882 - Expose CollectionPersister from AbstractCollectionEvent
1 parent 77a53bb commit 14956fd

File tree

8 files changed

+141
-88
lines changed

8 files changed

+141
-88
lines changed

hibernate-core/src/main/java/org/hibernate/event/spi/AbstractCollectionEvent.java

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,111 @@
1111
* Defines a base class for events involving collections.
1212
*
1313
* @author Gail Badner
14+
* @author Steve Ebersole
1415
*/
1516
public abstract class AbstractCollectionEvent extends AbstractSessionEvent {
16-
1717
private final PersistentCollection<?> collection;
18-
private final Object affectedOwner;
19-
private final Object affectedOwnerId;
20-
private final String affectedOwnerEntityName;
18+
private final CollectionPersister collectionPersister;
19+
private final Object owner;
20+
private final Object ownerId;
21+
private final String ownerEntityName;
2122

2223
/**
2324
* Constructs an instance for a stateful session.
24-
* @param collection - the collection
25-
* @param source - the Session source
26-
* @param affectedOwner - the owner that is affected by this event;
27-
* can be null if unavailable
28-
* @param affectedOwnerId - the ID for the owner that is affected
29-
* by this event; can be null if unavailable
25+
*
26+
* @param collectionPersister The descriptor for the collection mapping.
27+
* @param collection - The (wrapped) collection instance.
28+
* @param source - The {@linkplain org.hibernate.Session session}
29+
* @param owner - The entity instance that "owns" the {@code collection}
30+
* affected by this event; can be {@code null} if unavailable.
31+
* @param ownerId - The identifier value for the {@code owner}; can be
32+
* {@code null} if unavailable.
3033
*/
3134
public AbstractCollectionEvent(
3235
CollectionPersister collectionPersister,
3336
PersistentCollection<?> collection,
3437
EventSource source,
35-
Object affectedOwner,
36-
Object affectedOwnerId) {
38+
Object owner,
39+
Object ownerId) {
3740
super( source );
3841
this.collection = collection;
39-
this.affectedOwner = affectedOwner;
40-
this.affectedOwnerId = affectedOwnerId;
41-
this.affectedOwnerEntityName =
42-
getAffectedOwnerEntityName( collectionPersister, affectedOwner, source );
42+
this.collectionPersister = collectionPersister;
43+
this.owner = owner;
44+
this.ownerId = ownerId;
45+
this.ownerEntityName = getAffectedOwnerEntityName( collectionPersister, owner, source );
4346
}
4447

4548
/**
4649
* Constructs an instance for a stateless session.
47-
* @param collection - the collection
48-
* @param entityName - the name of the owning entity
49-
* @param affectedOwner - the owner that is affected by this event;
50-
* can be null if unavailable
51-
* @param affectedOwnerId - the ID for the owner that is affected
52-
* by this event; can be null if unavailable
50+
*
51+
* @param collectionPersister The descriptor for the collection mapping.
52+
* @param collection - The (wrapped) collection instance.
53+
* @param ownerEntityName - The entity-name of the {@code owner}.
54+
* @param owner - The entity instance that "owns" the {@code collection}
55+
* affected by this event; can be {@code null} if unavailable.
56+
* @param ownerId - The identifier value for the {@code owner}; can be
57+
* {@code null} if unavailable.
5358
*/
5459
public AbstractCollectionEvent(
60+
CollectionPersister collectionPersister,
5561
PersistentCollection<?> collection,
56-
String entityName,
57-
Object affectedOwner,
58-
Object affectedOwnerId) {
62+
String ownerEntityName,
63+
Object owner,
64+
Object ownerId) {
5965
super( null );
6066
this.collection = collection;
61-
this.affectedOwner = affectedOwner;
62-
this.affectedOwnerId = affectedOwnerId;
63-
this.affectedOwnerEntityName = entityName;
67+
this.owner = owner;
68+
this.ownerId = ownerId;
69+
this.ownerEntityName = ownerEntityName;
70+
this.collectionPersister = collectionPersister;
71+
}
72+
73+
/**
74+
* The descriptor for the {@linkplain #getCollection() collection} mapping.
75+
*/
76+
public CollectionPersister getCollectionPersister() {
77+
return collectionPersister;
78+
}
79+
80+
/**
81+
* The (wrapped) collection instance affected by this event.
82+
*/
83+
public PersistentCollection<?> getCollection() {
84+
return collection;
85+
}
86+
87+
/**
88+
* Get the collection owner entity that is affected by this event.
89+
*
90+
* @return the affected owner; returns null if the entity is not in the persistence context
91+
* (e.g., because the collection from a detached entity was moved to a new owner)
92+
*/
93+
public Object getAffectedOwnerOrNull() {
94+
return owner;
95+
}
96+
97+
/**
98+
* Get the ID for the collection owner entity that is affected by this event.
99+
*
100+
* @return the affected owner ID; returns null if the ID cannot be obtained
101+
* from the collection's loaded key (e.g., a property-ref is used for the
102+
* collection and does not include the entity's ID)
103+
*/
104+
public Object getAffectedOwnerIdOrNull() {
105+
return ownerId;
106+
}
107+
108+
/**
109+
* Get the entity name for the collection owner entity that is affected by this event.
110+
*
111+
* @return the entity name; if the owner is not in the PersistenceContext, the
112+
* returned value may be a superclass name, instead of the actual class name
113+
*/
114+
public String getAffectedOwnerEntityName() {
115+
return ownerEntityName;
64116
}
65117

66-
protected static CollectionPersister getLoadedCollectionPersister( PersistentCollection<?> collection, EventSource source ) {
118+
protected static CollectionPersister getLoadedCollectionPersister(PersistentCollection<?> collection, EventSource source) {
67119
final var entry = source.getPersistenceContextInternal().getCollectionEntry( collection );
68120
return entry == null ? null : entry.getLoadedPersister();
69121
}
@@ -102,39 +154,4 @@ protected static String getAffectedOwnerEntityName(
102154
: null;
103155
}
104156

105-
public PersistentCollection<?> getCollection() {
106-
return collection;
107-
}
108-
109-
/**
110-
* Get the collection owner entity that is affected by this event.
111-
*
112-
* @return the affected owner; returns null if the entity is not in the persistence context
113-
* (e.g., because the collection from a detached entity was moved to a new owner)
114-
*/
115-
public Object getAffectedOwnerOrNull() {
116-
return affectedOwner;
117-
}
118-
119-
/**
120-
* Get the ID for the collection owner entity that is affected by this event.
121-
*
122-
* @return the affected owner ID; returns null if the ID cannot be obtained
123-
* from the collection's loaded key (e.g., a property-ref is used for the
124-
* collection and does not include the entity's ID)
125-
*/
126-
public Object getAffectedOwnerIdOrNull() {
127-
return affectedOwnerId;
128-
}
129-
130-
/**
131-
* Get the entity name for the collection owner entity that is affected by this event.
132-
*
133-
* @return the entity name; if the owner is not in the PersistenceContext, the
134-
* returned value may be a superclass name, instead of the actual class name
135-
*/
136-
public String getAffectedOwnerEntityName() {
137-
return affectedOwnerEntityName;
138-
}
139-
140157
}

hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRecreateEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public PostCollectionRecreateEvent(
2828
}
2929

3030
public PostCollectionRecreateEvent(
31+
CollectionPersister collectionPersister,
3132
PersistentCollection<?> collection,
3233
Object id,
3334
String entityName,
3435
Object loadedOwner) {
35-
super( collection, entityName, loadedOwner, id );
36+
super( collectionPersister, collection, entityName, loadedOwner, id );
3637
}
3738
}

hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRemoveEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public PostCollectionRemoveEvent(
2828
}
2929

3030
public PostCollectionRemoveEvent(
31+
CollectionPersister collectionPersister,
3132
PersistentCollection<?> collection,
3233
Object id,
3334
String entityName,
3435
Object loadedOwner) {
35-
super( collection, entityName, loadedOwner, id );
36+
super( collectionPersister, collection, entityName, loadedOwner, id );
3637
}
3738
}

hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionUpdateEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public PostCollectionUpdateEvent(
2828

2929

3030
public PostCollectionUpdateEvent(
31+
CollectionPersister collectionPersister,
3132
PersistentCollection<?> collection,
3233
Object id,
3334
String entityName,
3435
Object loadedOwner) {
35-
super( collection, entityName, loadedOwner, id );
36+
super( collectionPersister, collection, entityName, loadedOwner, id );
3637
}
3738
}

hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRecreateEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public PreCollectionRecreateEvent(
2929

3030

3131
public PreCollectionRecreateEvent(
32+
CollectionPersister collectionPersister,
3233
PersistentCollection<?> collection,
3334
Object id,
3435
String entityName,
3536
Object loadedOwner) {
36-
super( collection, entityName, loadedOwner, id );
37+
super( collectionPersister, collection, entityName, loadedOwner, id );
3738
}
3839
}

hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRemoveEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public PreCollectionRemoveEvent(
2929
}
3030

3131
public PreCollectionRemoveEvent(
32+
CollectionPersister collectionPersister,
3233
PersistentCollection<?> collection,
3334
Object id,
3435
String entityName,
3536
Object loadedOwner) {
36-
super( collection, entityName, loadedOwner, id );
37+
super( collectionPersister, collection, entityName, loadedOwner, id );
3738
}
3839
}

hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionUpdateEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public PreCollectionUpdateEvent(
2929

3030

3131
public PreCollectionUpdateEvent(
32+
CollectionPersister collectionPersister,
3233
PersistentCollection<?> collection,
3334
Object id,
3435
String entityName,
3536
Object loadedOwner) {
36-
super( collection, entityName, loadedOwner, id );
37+
super( collectionPersister, collection, entityName, loadedOwner, id );
3738
}
3839
}

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

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private void recreateCollections(Object entity, Object id, EntityPersister persi
294294
forEachOwnedCollection( entity, id, persister,
295295
(descriptor, collection) -> {
296296
final String role = descriptor.getRole();
297-
firePreRecreate( collection, id, entityName, entity );
297+
firePreRecreate( descriptor, collection, id, entityName, entity );
298298
final var event = eventMonitor.beginCollectionRecreateEvent();
299299
boolean success = false;
300300
try {
@@ -307,7 +307,7 @@ private void recreateCollections(Object entity, Object id, EntityPersister persi
307307
if ( statistics.isStatisticsEnabled() ) {
308308
statistics.recreateCollection( role );
309309
}
310-
firePostRecreate( collection, id, entityName, entity );
310+
firePostRecreate( descriptor, collection, id, entityName, entity );
311311
} );
312312
}
313313
}
@@ -371,7 +371,7 @@ private void removeCollections(Object entity, Object id, EntityPersister persist
371371
forEachOwnedCollection( entity, id, persister,
372372
(descriptor, collection) -> {
373373
final String role = descriptor.getRole();
374-
firePreRemove( collection, id, entityName, entity );
374+
firePreRemove( descriptor, collection, id, entityName, entity );
375375
final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent();
376376
boolean success = false;
377377
try {
@@ -381,7 +381,7 @@ private void removeCollections(Object entity, Object id, EntityPersister persist
381381
finally {
382382
eventMonitor.completeCollectionRemoveEvent( event, id, role, success, this );
383383
}
384-
firePostRemove( collection, id, entityName, entity );
384+
firePostRemove( descriptor, collection, id, entityName, entity );
385385
if ( statistics.isStatisticsEnabled() ) {
386386
statistics.removeCollection( role );
387387
}
@@ -459,7 +459,7 @@ private void removeAndRecreateCollections(Object entity, Object id, EntityPersis
459459
forEachOwnedCollection( entity, id, persister,
460460
(descriptor, collection) -> {
461461
final String role = descriptor.getRole();
462-
firePreUpdate( collection, id, entityName, entity );
462+
firePreUpdate( descriptor, collection, id, entityName, entity );
463463
final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent();
464464
boolean success = false;
465465
try {
@@ -471,7 +471,7 @@ private void removeAndRecreateCollections(Object entity, Object id, EntityPersis
471471
finally {
472472
eventMonitor.completeCollectionRemoveEvent( event, id, role, success, this );
473473
}
474-
firePostUpdate( collection, id, entityName, entity );
474+
firePostUpdate( descriptor, collection, id, entityName, entity );
475475
if ( statistics.isStatisticsEnabled() ) {
476476
statistics.updateCollection( role );
477477
}
@@ -668,44 +668,74 @@ protected void firePostDelete(Object entity, Object id, EntityPersister persiste
668668
}
669669

670670
// Hibernate Reactive may need to call this
671-
protected void firePreRecreate(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
671+
protected void firePreRecreate(
672+
CollectionPersister collectionPersister,
673+
PersistentCollection<?> collection,
674+
Object id,
675+
String entityName,
676+
Object owner) {
672677
eventListenerGroups.eventListenerGroup_PRE_COLLECTION_RECREATE.fireLazyEventOnEachListener(
673-
() -> new PreCollectionRecreateEvent( collection, id, entityName, owner ),
678+
() -> new PreCollectionRecreateEvent( collectionPersister, collection, id, entityName, owner ),
674679
PreCollectionRecreateEventListener::onPreRecreateCollection );
675680
}
676681

677682
// Hibernate Reactive may need to call this
678-
protected void firePreUpdate(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
683+
protected void firePreUpdate(
684+
CollectionPersister collectionPersister,
685+
PersistentCollection<?> collection,
686+
Object id,
687+
String entityName,
688+
Object owner) {
679689
eventListenerGroups.eventListenerGroup_PRE_COLLECTION_UPDATE.fireLazyEventOnEachListener(
680-
() -> new PreCollectionUpdateEvent( collection, id, entityName, owner ),
690+
() -> new PreCollectionUpdateEvent( collectionPersister, collection, id, entityName, owner ),
681691
PreCollectionUpdateEventListener::onPreUpdateCollection );
682692
}
683693

684694
// Hibernate Reactive may need to call this
685-
protected void firePreRemove(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
695+
protected void firePreRemove(
696+
CollectionPersister collectionPersister,
697+
PersistentCollection<?> collection,
698+
Object id,
699+
String entityName,
700+
Object owner) {
686701
eventListenerGroups.eventListenerGroup_PRE_COLLECTION_REMOVE.fireLazyEventOnEachListener(
687-
() -> new PreCollectionRemoveEvent( collection, id, entityName, owner ),
702+
() -> new PreCollectionRemoveEvent( collectionPersister, collection, id, entityName, owner ),
688703
PreCollectionRemoveEventListener::onPreRemoveCollection );
689704
}
690705

691706
// Hibernate Reactive may need to call this
692-
protected void firePostRecreate(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
707+
protected void firePostRecreate(
708+
CollectionPersister collectionPersister,
709+
PersistentCollection<?> collection,
710+
Object id,
711+
String entityName,
712+
Object owner) {
693713
eventListenerGroups.eventListenerGroup_POST_COLLECTION_RECREATE.fireLazyEventOnEachListener(
694-
() -> new PostCollectionRecreateEvent( collection, id, entityName, owner ),
714+
() -> new PostCollectionRecreateEvent( collectionPersister, collection, id, entityName, owner ),
695715
PostCollectionRecreateEventListener::onPostRecreateCollection );
696716
}
697717

698718
// Hibernate Reactive may need to call this
699-
protected void firePostUpdate(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
719+
protected void firePostUpdate(
720+
CollectionPersister collectionPersister,
721+
PersistentCollection<?> collection,
722+
Object id,
723+
String entityName,
724+
Object owner) {
700725
eventListenerGroups.eventListenerGroup_POST_COLLECTION_UPDATE.fireLazyEventOnEachListener(
701-
() -> new PostCollectionUpdateEvent( collection, id, entityName, owner ),
726+
() -> new PostCollectionUpdateEvent( collectionPersister, collection, id, entityName, owner ),
702727
PostCollectionUpdateEventListener::onPostUpdateCollection );
703728
}
704729

705730
// Hibernate Reactive may need to call this
706-
protected void firePostRemove(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
731+
protected void firePostRemove(
732+
CollectionPersister collectionPersister,
733+
PersistentCollection<?> collection,
734+
Object id,
735+
String entityName,
736+
Object owner) {
707737
eventListenerGroups.eventListenerGroup_POST_COLLECTION_REMOVE.fireLazyEventOnEachListener(
708-
() -> new PostCollectionRemoveEvent( collection, id, entityName, owner ),
738+
() -> new PostCollectionRemoveEvent( collectionPersister, collection, id, entityName, owner ),
709739
PostCollectionRemoveEventListener::onPostRemoveCollection );
710740
}
711741

0 commit comments

Comments
 (0)