Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 );
} );
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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 );
}

Expand Down