Question about interceptor postFlush method


Hi, I have implemented a custom Interceptor in order to do some logging. My problem is that after deleting an entity the postFlush method is called without any entities on the Iterator. Is this normal?


That ultimately depends on what managed objects are being loaded and operated upon during your transaction.

If your transaction loaded an entity and immediately followed by removing it as follows

final SimpleEntity entity = entityManager.find( SimpleEntity.class, entityId );
entityManager.remove( entity );

This results in the EntityDeleteAction removing the entity from the PersistenceContext when you remove the entity so when the flush fires, the interceptor is given an empty iterator because the context is technically empty due to the operations that took place.

But if your transaction had done something more like this:

final SimpleEntity entity1 = entityManager.find( SimpleEntity.class, entityId1 );
final SimpleEntity entity2 = entityManager.find( SimpleEntity.class, entityId2 );
entityManager.remove( entity1 );

The PersistenceContext would hold 2 entities which you later remove only 1. This would result in the flush event providing the interceptor with an iterator that holds a single reference, entity2.