diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/AbstractCollectionEvent.java b/hibernate-core/src/main/java/org/hibernate/event/spi/AbstractCollectionEvent.java index 9ffbe16cd706..20d6e5d4c80a 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/AbstractCollectionEvent.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/AbstractCollectionEvent.java @@ -11,59 +11,111 @@ * Defines a base class for events involving collections. * * @author Gail Badner + * @author Steve Ebersole */ public abstract class AbstractCollectionEvent extends AbstractSessionEvent { - private final PersistentCollection collection; - private final Object affectedOwner; - private final Object affectedOwnerId; - private final String affectedOwnerEntityName; + private final CollectionPersister collectionPersister; + private final Object owner; + private final Object ownerId; + private final String ownerEntityName; /** * Constructs an instance for a stateful session. - * @param collection - the collection - * @param source - the Session source - * @param affectedOwner - the owner that is affected by this event; - * can be null if unavailable - * @param affectedOwnerId - the ID for the owner that is affected - * by this event; can be null if unavailable + * + * @param collectionPersister The descriptor for the collection mapping. + * @param collection - The (wrapped) collection instance. + * @param source - The {@linkplain org.hibernate.Session session} + * @param owner - The entity instance that "owns" the {@code collection} + * affected by this event; can be {@code null} if unavailable. + * @param ownerId - The identifier value for the {@code owner}; can be + * {@code null} if unavailable. */ public AbstractCollectionEvent( CollectionPersister collectionPersister, PersistentCollection collection, EventSource source, - Object affectedOwner, - Object affectedOwnerId) { + Object owner, + Object ownerId) { super( source ); this.collection = collection; - this.affectedOwner = affectedOwner; - this.affectedOwnerId = affectedOwnerId; - this.affectedOwnerEntityName = - getAffectedOwnerEntityName( collectionPersister, affectedOwner, source ); + this.collectionPersister = collectionPersister; + this.owner = owner; + this.ownerId = ownerId; + this.ownerEntityName = getAffectedOwnerEntityName( collectionPersister, owner, source ); } /** * Constructs an instance for a stateless session. - * @param collection - the collection - * @param entityName - the name of the owning entity - * @param affectedOwner - the owner that is affected by this event; - * can be null if unavailable - * @param affectedOwnerId - the ID for the owner that is affected - * by this event; can be null if unavailable + * + * @param collectionPersister The descriptor for the collection mapping. + * @param collection - The (wrapped) collection instance. + * @param ownerEntityName - The entity-name of the {@code owner}. + * @param owner - The entity instance that "owns" the {@code collection} + * affected by this event; can be {@code null} if unavailable. + * @param ownerId - The identifier value for the {@code owner}; can be + * {@code null} if unavailable. */ public AbstractCollectionEvent( + CollectionPersister collectionPersister, PersistentCollection collection, - String entityName, - Object affectedOwner, - Object affectedOwnerId) { + String ownerEntityName, + Object owner, + Object ownerId) { super( null ); this.collection = collection; - this.affectedOwner = affectedOwner; - this.affectedOwnerId = affectedOwnerId; - this.affectedOwnerEntityName = entityName; + this.owner = owner; + this.ownerId = ownerId; + this.ownerEntityName = ownerEntityName; + this.collectionPersister = collectionPersister; + } + + /** + * The descriptor for the {@linkplain #getCollection() collection} mapping. + */ + public CollectionPersister getCollectionPersister() { + return collectionPersister; + } + + /** + * The (wrapped) collection instance affected by this event. + */ + public PersistentCollection getCollection() { + return collection; + } + + /** + * Get the collection owner entity that is affected by this event. + * + * @return the affected owner; returns null if the entity is not in the persistence context + * (e.g., because the collection from a detached entity was moved to a new owner) + */ + public Object getAffectedOwnerOrNull() { + return owner; + } + + /** + * Get the ID for the collection owner entity that is affected by this event. + * + * @return the affected owner ID; returns null if the ID cannot be obtained + * from the collection's loaded key (e.g., a property-ref is used for the + * collection and does not include the entity's ID) + */ + public Object getAffectedOwnerIdOrNull() { + return ownerId; + } + + /** + * Get the entity name for the collection owner entity that is affected by this event. + * + * @return the entity name; if the owner is not in the PersistenceContext, the + * returned value may be a superclass name, instead of the actual class name + */ + public String getAffectedOwnerEntityName() { + return ownerEntityName; } - protected static CollectionPersister getLoadedCollectionPersister( PersistentCollection collection, EventSource source ) { + protected static CollectionPersister getLoadedCollectionPersister(PersistentCollection collection, EventSource source) { final var entry = source.getPersistenceContextInternal().getCollectionEntry( collection ); return entry == null ? null : entry.getLoadedPersister(); } @@ -102,39 +154,4 @@ protected static String getAffectedOwnerEntityName( : null; } - public PersistentCollection getCollection() { - return collection; - } - - /** - * Get the collection owner entity that is affected by this event. - * - * @return the affected owner; returns null if the entity is not in the persistence context - * (e.g., because the collection from a detached entity was moved to a new owner) - */ - public Object getAffectedOwnerOrNull() { - return affectedOwner; - } - - /** - * Get the ID for the collection owner entity that is affected by this event. - * - * @return the affected owner ID; returns null if the ID cannot be obtained - * from the collection's loaded key (e.g., a property-ref is used for the - * collection and does not include the entity's ID) - */ - public Object getAffectedOwnerIdOrNull() { - return affectedOwnerId; - } - - /** - * Get the entity name for the collection owner entity that is affected by this event. - * - * @return the entity name; if the owner is not in the PersistenceContext, the - * returned value may be a superclass name, instead of the actual class name - */ - public String getAffectedOwnerEntityName() { - return affectedOwnerEntityName; - } - } diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRecreateEvent.java b/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRecreateEvent.java index 6a5357a98705..d449e799d834 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRecreateEvent.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRecreateEvent.java @@ -28,10 +28,11 @@ public PostCollectionRecreateEvent( } public PostCollectionRecreateEvent( + CollectionPersister collectionPersister, PersistentCollection collection, Object id, String entityName, Object loadedOwner) { - super( collection, entityName, loadedOwner, id ); + super( collectionPersister, collection, entityName, loadedOwner, id ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRemoveEvent.java b/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRemoveEvent.java index 96061a54fd74..55aa0d0366ad 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRemoveEvent.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionRemoveEvent.java @@ -28,10 +28,11 @@ public PostCollectionRemoveEvent( } public PostCollectionRemoveEvent( + CollectionPersister collectionPersister, PersistentCollection collection, Object id, String entityName, Object loadedOwner) { - super( collection, entityName, loadedOwner, id ); + super( collectionPersister, collection, entityName, loadedOwner, id ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionUpdateEvent.java b/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionUpdateEvent.java index c0a3569451d8..ab3ce79a3f27 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionUpdateEvent.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/PostCollectionUpdateEvent.java @@ -28,10 +28,11 @@ public PostCollectionUpdateEvent( public PostCollectionUpdateEvent( + CollectionPersister collectionPersister, PersistentCollection collection, Object id, String entityName, Object loadedOwner) { - super( collection, entityName, loadedOwner, id ); + super( collectionPersister, collection, entityName, loadedOwner, id ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRecreateEvent.java b/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRecreateEvent.java index 68cd61f9eab0..ae9f62cd4764 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRecreateEvent.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRecreateEvent.java @@ -29,10 +29,11 @@ public PreCollectionRecreateEvent( public PreCollectionRecreateEvent( + CollectionPersister collectionPersister, PersistentCollection collection, Object id, String entityName, Object loadedOwner) { - super( collection, entityName, loadedOwner, id ); + super( collectionPersister, collection, entityName, loadedOwner, id ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRemoveEvent.java b/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRemoveEvent.java index 84a458bb9df3..ea816de2dfb0 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRemoveEvent.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionRemoveEvent.java @@ -29,10 +29,11 @@ public PreCollectionRemoveEvent( } public PreCollectionRemoveEvent( + CollectionPersister collectionPersister, PersistentCollection collection, Object id, String entityName, Object loadedOwner) { - super( collection, entityName, loadedOwner, id ); + super( collectionPersister, collection, entityName, loadedOwner, id ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionUpdateEvent.java b/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionUpdateEvent.java index 2aea2a8becad..954c255bd8a2 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionUpdateEvent.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/PreCollectionUpdateEvent.java @@ -29,10 +29,11 @@ public PreCollectionUpdateEvent( public PreCollectionUpdateEvent( + CollectionPersister collectionPersister, PersistentCollection collection, Object id, String entityName, Object loadedOwner) { - super( collection, entityName, loadedOwner, id ); + super( collectionPersister, collection, entityName, loadedOwner, id ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java index 778a14a1c543..c2992e710f3b 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java @@ -294,7 +294,7 @@ private void recreateCollections(Object entity, Object id, EntityPersister persi forEachOwnedCollection( entity, id, persister, (descriptor, collection) -> { final String role = descriptor.getRole(); - firePreRecreate( collection, id, entityName, entity ); + firePreRecreate( descriptor, collection, id, entityName, entity ); final var event = eventMonitor.beginCollectionRecreateEvent(); boolean success = false; try { @@ -307,7 +307,7 @@ private void recreateCollections(Object entity, Object id, EntityPersister persi if ( statistics.isStatisticsEnabled() ) { statistics.recreateCollection( role ); } - firePostRecreate( collection, id, entityName, entity ); + firePostRecreate( descriptor, collection, id, entityName, entity ); } ); } } @@ -371,7 +371,7 @@ private void removeCollections(Object entity, Object id, EntityPersister persist forEachOwnedCollection( entity, id, persister, (descriptor, collection) -> { final String role = descriptor.getRole(); - firePreRemove( collection, id, entityName, entity ); + firePreRemove( descriptor, collection, id, entityName, entity ); final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent(); boolean success = false; try { @@ -381,7 +381,7 @@ private void removeCollections(Object entity, Object id, EntityPersister persist finally { eventMonitor.completeCollectionRemoveEvent( event, id, role, success, this ); } - firePostRemove( collection, id, entityName, entity ); + firePostRemove( descriptor, collection, id, entityName, entity ); if ( statistics.isStatisticsEnabled() ) { statistics.removeCollection( role ); } @@ -459,7 +459,7 @@ private void removeAndRecreateCollections(Object entity, Object id, EntityPersis forEachOwnedCollection( entity, id, persister, (descriptor, collection) -> { final String role = descriptor.getRole(); - firePreUpdate( collection, id, entityName, entity ); + firePreUpdate( descriptor, collection, id, entityName, entity ); final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent(); boolean success = false; try { @@ -471,7 +471,7 @@ private void removeAndRecreateCollections(Object entity, Object id, EntityPersis finally { eventMonitor.completeCollectionRemoveEvent( event, id, role, success, this ); } - firePostUpdate( collection, id, entityName, entity ); + firePostUpdate( descriptor, collection, id, entityName, entity ); if ( statistics.isStatisticsEnabled() ) { statistics.updateCollection( role ); } @@ -668,44 +668,74 @@ protected void firePostDelete(Object entity, Object id, EntityPersister persiste } // Hibernate Reactive may need to call this - protected void firePreRecreate(PersistentCollection collection, Object id, String entityName, Object owner) { + protected void firePreRecreate( + CollectionPersister collectionPersister, + PersistentCollection collection, + Object id, + String entityName, + Object owner) { eventListenerGroups.eventListenerGroup_PRE_COLLECTION_RECREATE.fireLazyEventOnEachListener( - () -> new PreCollectionRecreateEvent( collection, id, entityName, owner ), + () -> new PreCollectionRecreateEvent( collectionPersister, collection, id, entityName, owner ), PreCollectionRecreateEventListener::onPreRecreateCollection ); } // Hibernate Reactive may need to call this - protected void firePreUpdate(PersistentCollection collection, Object id, String entityName, Object owner) { + protected void firePreUpdate( + CollectionPersister collectionPersister, + PersistentCollection collection, + Object id, + String entityName, + Object owner) { eventListenerGroups.eventListenerGroup_PRE_COLLECTION_UPDATE.fireLazyEventOnEachListener( - () -> new PreCollectionUpdateEvent( collection, id, entityName, owner ), + () -> new PreCollectionUpdateEvent( collectionPersister, collection, id, entityName, owner ), PreCollectionUpdateEventListener::onPreUpdateCollection ); } // Hibernate Reactive may need to call this - protected void firePreRemove(PersistentCollection collection, Object id, String entityName, Object owner) { + protected void firePreRemove( + CollectionPersister collectionPersister, + PersistentCollection collection, + Object id, + String entityName, + Object owner) { eventListenerGroups.eventListenerGroup_PRE_COLLECTION_REMOVE.fireLazyEventOnEachListener( - () -> new PreCollectionRemoveEvent( collection, id, entityName, owner ), + () -> new PreCollectionRemoveEvent( collectionPersister, collection, id, entityName, owner ), PreCollectionRemoveEventListener::onPreRemoveCollection ); } // Hibernate Reactive may need to call this - protected void firePostRecreate(PersistentCollection collection, Object id, String entityName, Object owner) { + protected void firePostRecreate( + CollectionPersister collectionPersister, + PersistentCollection collection, + Object id, + String entityName, + Object owner) { eventListenerGroups.eventListenerGroup_POST_COLLECTION_RECREATE.fireLazyEventOnEachListener( - () -> new PostCollectionRecreateEvent( collection, id, entityName, owner ), + () -> new PostCollectionRecreateEvent( collectionPersister, collection, id, entityName, owner ), PostCollectionRecreateEventListener::onPostRecreateCollection ); } // Hibernate Reactive may need to call this - protected void firePostUpdate(PersistentCollection collection, Object id, String entityName, Object owner) { + protected void firePostUpdate( + CollectionPersister collectionPersister, + PersistentCollection collection, + Object id, + String entityName, + Object owner) { eventListenerGroups.eventListenerGroup_POST_COLLECTION_UPDATE.fireLazyEventOnEachListener( - () -> new PostCollectionUpdateEvent( collection, id, entityName, owner ), + () -> new PostCollectionUpdateEvent( collectionPersister, collection, id, entityName, owner ), PostCollectionUpdateEventListener::onPostUpdateCollection ); } // Hibernate Reactive may need to call this - protected void firePostRemove(PersistentCollection collection, Object id, String entityName, Object owner) { + protected void firePostRemove( + CollectionPersister collectionPersister, + PersistentCollection collection, + Object id, + String entityName, + Object owner) { eventListenerGroups.eventListenerGroup_POST_COLLECTION_REMOVE.fireLazyEventOnEachListener( - () -> new PostCollectionRemoveEvent( collection, id, entityName, owner ), + () -> new PostCollectionRemoveEvent( collectionPersister, collection, id, entityName, owner ), PostCollectionRemoveEventListener::onPostRemoveCollection ); }