Skip to content

Commit eff4ab0

Browse files
committed
HHH-19523 correctly initialize Pre/PostCollectionXxxxEvents from StatelessSession
note that this change breaks Hibernate Reactive
1 parent cb1ead6 commit eff4ab0

File tree

8 files changed

+167
-77
lines changed

8 files changed

+167
-77
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public abstract class AbstractCollectionEvent extends AbstractEvent {
2222
private final String affectedOwnerEntityName;
2323

2424
/**
25-
* Constructs an AbstractCollectionEvent object.
26-
* @param collection - the collection
25+
* Constructs an instance for a stateful session.
26+
* @param collection - the collection
2727
* @param source - the Session source
2828
* @param affectedOwner - the owner that is affected by this event;
29-
* can be null if unavailable
29+
* can be null if unavailable
3030
* @param affectedOwnerId - the ID for the owner that is affected
31-
* by this event; can be null if unavailable
31+
* by this event; can be null if unavailable
3232
*/
3333
public AbstractCollectionEvent(
3434
CollectionPersister collectionPersister,
@@ -44,6 +44,27 @@ public AbstractCollectionEvent(
4444
getAffectedOwnerEntityName( collectionPersister, affectedOwner, source );
4545
}
4646

47+
/**
48+
* Constructs an instance for a stateless session.
49+
* @param collection - the collection
50+
* @param entityName - the name of the owning entity
51+
* @param affectedOwner - the owner that is affected by this event;
52+
* can be null if unavailable
53+
* @param affectedOwnerId - the ID for the owner that is affected
54+
* by this event; can be null if unavailable
55+
*/
56+
public AbstractCollectionEvent(
57+
PersistentCollection<?> collection,
58+
String entityName,
59+
Object affectedOwner,
60+
Object affectedOwnerId) {
61+
super( null );
62+
this.collection = collection;
63+
this.affectedOwner = affectedOwner;
64+
this.affectedOwnerId = affectedOwnerId;
65+
this.affectedOwnerEntityName = entityName;
66+
}
67+
4768
protected static CollectionPersister getLoadedCollectionPersister( PersistentCollection<?> collection, EventSource source ) {
4869
CollectionEntry ce = source.getPersistenceContextInternal().getCollectionEntry( collection );
4970
return ce == null ? null : ce.getLoadedPersister();
@@ -58,7 +79,7 @@ protected static Object getLoadedOwnerIdOrNull(PersistentCollection<?> collectio
5879
}
5980

6081
protected static Object getOwnerIdOrNull(Object owner, EventSource source ) {
61-
EntityEntry ownerEntry = source.getPersistenceContextInternal().getEntry( owner );
82+
final EntityEntry ownerEntry = source.getPersistenceContextInternal().getEntry( owner );
6283
return ownerEntry == null ? null : ownerEntry.getId();
6384
}
6485

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ public PostCollectionRecreateEvent(
2626
getOwnerIdOrNull( collection.getOwner(), source )
2727
);
2828
}
29+
30+
public PostCollectionRecreateEvent(
31+
PersistentCollection<?> collection,
32+
Object id,
33+
String entityName,
34+
Object loadedOwner) {
35+
super( collection, entityName, loadedOwner, id );
36+
}
2937
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ public PostCollectionRemoveEvent(
1818
PersistentCollection<?> collection,
1919
EventSource source,
2020
Object loadedOwner) {
21-
super( collectionPersister, collection, source, loadedOwner, getOwnerIdOrNull( loadedOwner, source ) );
21+
super(
22+
collectionPersister,
23+
collection,
24+
source,
25+
loadedOwner,
26+
getOwnerIdOrNull( loadedOwner, source )
27+
);
28+
}
29+
30+
public PostCollectionRemoveEvent(
31+
PersistentCollection<?> collection,
32+
Object id,
33+
String entityName,
34+
Object loadedOwner) {
35+
super( collection, entityName, loadedOwner, id );
2236
}
2337
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ public PostCollectionUpdateEvent(
2525
getLoadedOwnerIdOrNull( collection, source )
2626
);
2727
}
28+
29+
30+
public PostCollectionUpdateEvent(
31+
PersistentCollection<?> collection,
32+
Object id,
33+
String entityName,
34+
Object loadedOwner) {
35+
super( collection, entityName, loadedOwner, id );
36+
}
2837
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,13 @@ public PreCollectionRecreateEvent(
2626
getOwnerIdOrNull( collection.getOwner(), source )
2727
);
2828
}
29+
30+
31+
public PreCollectionRecreateEvent(
32+
PersistentCollection<?> collection,
33+
Object id,
34+
String entityName,
35+
Object loadedOwner) {
36+
super( collection, entityName, loadedOwner, id );
37+
}
2938
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ public PreCollectionRemoveEvent(
2727
getOwnerIdOrNull( loadedOwner, source )
2828
);
2929
}
30+
31+
public PreCollectionRemoveEvent(
32+
PersistentCollection<?> collection,
33+
Object id,
34+
String entityName,
35+
Object loadedOwner) {
36+
super( collection, entityName, loadedOwner, id );
37+
}
3038
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,13 @@ public PreCollectionUpdateEvent(
2626
getLoadedOwnerIdOrNull( collection, source )
2727
);
2828
}
29+
30+
31+
public PreCollectionUpdateEvent(
32+
PersistentCollection<?> collection,
33+
Object id,
34+
String entityName,
35+
Object loadedOwner) {
36+
super( collection, entityName, loadedOwner, id );
37+
}
2938
}

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

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -275,25 +275,29 @@ else if ( generator.generatedOnExecution( entity, this ) ) {
275275
}
276276

277277
private void recreateCollections(Object entity, Object id, EntityPersister persister) {
278-
forEachOwnedCollection( entity, id, persister,
279-
(descriptor, collection) -> {
280-
firePreRecreate( collection, descriptor );
281-
final EventMonitor eventMonitor = getEventMonitor();
282-
final DiagnosticEvent event = eventMonitor.beginCollectionRecreateEvent();
283-
boolean success = false;
284-
try {
285-
descriptor.recreate( collection, id, this );
286-
success = true;
287-
}
288-
finally {
289-
eventMonitor.completeCollectionRecreateEvent( event, id, descriptor.getRole(), success, this );
290-
}
291-
final StatisticsImplementor statistics = getFactory().getStatistics();
292-
if ( statistics.isStatisticsEnabled() ) {
293-
statistics.recreateCollection( descriptor.getRole() );
294-
}
295-
firePostRecreate( collection, descriptor );
296-
} );
278+
if ( persister.hasOwnedCollections() ) {
279+
final String entityName = persister.getEntityName();
280+
final EventMonitor eventMonitor = getEventMonitor();
281+
final StatisticsImplementor statistics = getFactory().getStatistics();
282+
forEachOwnedCollection( entity, id, persister,
283+
(descriptor, collection) -> {
284+
final String role = descriptor.getRole();
285+
firePreRecreate( collection, id, entityName, entity );
286+
final DiagnosticEvent event = eventMonitor.beginCollectionRecreateEvent();
287+
boolean success = false;
288+
try {
289+
descriptor.recreate( collection, id, this );
290+
success = true;
291+
}
292+
finally {
293+
eventMonitor.completeCollectionRecreateEvent( event, id, role, success, this );
294+
}
295+
if ( statistics.isStatisticsEnabled() ) {
296+
statistics.recreateCollection( role );
297+
}
298+
firePostRecreate( collection, id, entityName, entity );
299+
} );
300+
}
297301
}
298302

299303
// deletes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -347,25 +351,29 @@ public void delete(String entityName, Object entity) {
347351
}
348352

349353
private void removeCollections(Object entity, Object id, EntityPersister persister) {
350-
forEachOwnedCollection( entity, id, persister,
351-
(descriptor, collection) -> {
352-
firePreRemove( collection, entity, descriptor );
353-
final EventMonitor eventMonitor = getEventMonitor();
354-
final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent();
355-
boolean success = false;
356-
try {
357-
descriptor.remove( id, this );
358-
success = true;
359-
}
360-
finally {
361-
eventMonitor.completeCollectionRemoveEvent( event, id, descriptor.getRole(), success, this );
362-
}
363-
firePostRemove( collection, entity, descriptor );
364-
final StatisticsImplementor statistics = getFactory().getStatistics();
365-
if ( statistics.isStatisticsEnabled() ) {
366-
statistics.removeCollection( descriptor.getRole() );
367-
}
368-
} );
354+
if ( persister.hasOwnedCollections() ) {
355+
final String entityName = persister.getEntityName();
356+
final EventMonitor eventMonitor = getEventMonitor();
357+
final StatisticsImplementor statistics = getFactory().getStatistics();
358+
forEachOwnedCollection( entity, id, persister,
359+
(descriptor, collection) -> {
360+
final String role = descriptor.getRole();
361+
firePreRemove( collection, id, entityName, entity );
362+
final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent();
363+
boolean success = false;
364+
try {
365+
descriptor.remove( id, this );
366+
success = true;
367+
}
368+
finally {
369+
eventMonitor.completeCollectionRemoveEvent( event, id, role, success, this );
370+
}
371+
firePostRemove( collection, id, entityName, entity );
372+
if ( statistics.isStatisticsEnabled() ) {
373+
statistics.removeCollection( role );
374+
}
375+
} );
376+
}
369377
}
370378

371379

@@ -430,27 +438,31 @@ public void update(String entityName, Object entity) {
430438
}
431439

432440
private void removeAndRecreateCollections(Object entity, Object id, EntityPersister persister) {
433-
forEachOwnedCollection( entity, id, persister,
434-
(descriptor, collection) -> {
435-
firePreUpdate( collection, descriptor );
436-
final EventMonitor eventMonitor = getEventMonitor();
437-
final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent();
438-
boolean success = false;
439-
try {
440-
// TODO: can we do better here?
441-
descriptor.remove( id, this );
442-
descriptor.recreate( collection, id, this );
443-
success = true;
444-
}
445-
finally {
446-
eventMonitor.completeCollectionRemoveEvent( event, id, descriptor.getRole(), success, this );
447-
}
448-
firePostUpdate( collection, descriptor );
449-
final StatisticsImplementor statistics = getFactory().getStatistics();
450-
if ( statistics.isStatisticsEnabled() ) {
451-
statistics.updateCollection( descriptor.getRole() );
452-
}
453-
} );
441+
if ( persister.hasOwnedCollections() ) {
442+
final String entityName = persister.getEntityName();
443+
final EventMonitor eventMonitor = getEventMonitor();
444+
final StatisticsImplementor statistics = getFactory().getStatistics();
445+
forEachOwnedCollection( entity, id, persister,
446+
(descriptor, collection) -> {
447+
final String role = descriptor.getRole();
448+
firePreUpdate( collection, id, entityName, entity );
449+
final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent();
450+
boolean success = false;
451+
try {
452+
// TODO: can we do better here?
453+
descriptor.remove( id, this );
454+
descriptor.recreate( collection, id, this );
455+
success = true;
456+
}
457+
finally {
458+
eventMonitor.completeCollectionRemoveEvent( event, id, role, success, this );
459+
}
460+
firePostUpdate( collection, id, entityName, entity );
461+
if ( statistics.isStatisticsEnabled() ) {
462+
statistics.updateCollection( role );
463+
}
464+
} );
465+
}
454466
}
455467

456468
@Override
@@ -635,44 +647,44 @@ protected void firePostDelete(Object entity, Object id, EntityPersister persiste
635647
}
636648

637649
// Hibernate Reactive may need to call this
638-
protected void firePreRecreate(PersistentCollection<?> collection, CollectionPersister persister) {
650+
protected void firePreRecreate(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
639651
eventListenerGroups.eventListenerGroup_PRE_COLLECTION_RECREATE.fireLazyEventOnEachListener(
640-
() -> new PreCollectionRecreateEvent( persister, collection, null ),
652+
() -> new PreCollectionRecreateEvent( collection, id, entityName, owner ),
641653
PreCollectionRecreateEventListener::onPreRecreateCollection );
642654
}
643655

644656
// Hibernate Reactive may need to call this
645-
protected void firePreUpdate(PersistentCollection<?> collection, CollectionPersister persister) {
657+
protected void firePreUpdate(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
646658
eventListenerGroups.eventListenerGroup_PRE_COLLECTION_UPDATE.fireLazyEventOnEachListener(
647-
() -> new PreCollectionUpdateEvent( persister, collection, null ),
659+
() -> new PreCollectionUpdateEvent( collection, id, entityName, owner ),
648660
PreCollectionUpdateEventListener::onPreUpdateCollection );
649661
}
650662

651663
// Hibernate Reactive may need to call this
652-
protected void firePreRemove(PersistentCollection<?> collection, Object owner, CollectionPersister persister) {
664+
protected void firePreRemove(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
653665
eventListenerGroups.eventListenerGroup_PRE_COLLECTION_REMOVE.fireLazyEventOnEachListener(
654-
() -> new PreCollectionRemoveEvent( persister, collection, null, owner ),
666+
() -> new PreCollectionRemoveEvent( collection, id, entityName, owner ),
655667
PreCollectionRemoveEventListener::onPreRemoveCollection );
656668
}
657669

658670
// Hibernate Reactive may need to call this
659-
protected void firePostRecreate(PersistentCollection<?> collection, CollectionPersister persister) {
671+
protected void firePostRecreate(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
660672
eventListenerGroups.eventListenerGroup_POST_COLLECTION_RECREATE.fireLazyEventOnEachListener(
661-
() -> new PostCollectionRecreateEvent( persister, collection, null ),
673+
() -> new PostCollectionRecreateEvent( collection, id, entityName, owner ),
662674
PostCollectionRecreateEventListener::onPostRecreateCollection );
663675
}
664676

665677
// Hibernate Reactive may need to call this
666-
protected void firePostUpdate(PersistentCollection<?> collection, CollectionPersister persister) {
678+
protected void firePostUpdate(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
667679
eventListenerGroups.eventListenerGroup_POST_COLLECTION_UPDATE.fireLazyEventOnEachListener(
668-
() -> new PostCollectionUpdateEvent( persister, collection, null ),
680+
() -> new PostCollectionUpdateEvent( collection, id, entityName, owner ),
669681
PostCollectionUpdateEventListener::onPostUpdateCollection );
670682
}
671683

672684
// Hibernate Reactive may need to call this
673-
protected void firePostRemove(PersistentCollection<?> collection, Object owner, CollectionPersister persister) {
685+
protected void firePostRemove(PersistentCollection<?> collection, Object id, String entityName, Object owner) {
674686
eventListenerGroups.eventListenerGroup_POST_COLLECTION_REMOVE.fireLazyEventOnEachListener(
675-
() -> new PostCollectionRemoveEvent( persister, collection, null, owner ),
687+
() -> new PostCollectionRemoveEvent( collection, id, entityName, owner ),
676688
PostCollectionRemoveEventListener::onPostRemoveCollection );
677689
}
678690

0 commit comments

Comments
 (0)