Hibernate PostInsertEventListener call after flash

I have a class that implements a Hibernate PostInsertEventListener that is bound to a Hibernate SessionFactory through Spring. An interceptor is specified for the post-insert event type. This interceptor is called whenever I execute session.save(entity) . However, execution occurs before the transaction is cleared. This is problematic if the database throws an exception.

Question: Is there a way that I can specify to call the interceptor after session.flush() was called?

I tried to execute insertEvent.getSession().flush() inside the event listener, but this throws a primary key violation because the identifier is not set yet.

I have not yet learned the registerSynchronization method, because it seems to me that I am missing a basic configuration. The transaction manager used is org.springframework.orm.hibernate3.HibernateTransactionManager , and transaction boundaries are determined by the @Transactional annotation. Version for sleep mode 3.6.

+4
source share
3 answers

Current solution:

Save the Post event (Insert | Update | Delete) on the map with the session as the key. Get the event from the map in the FlushEventListener for the session and handle the event.

The card used is a Guava Cache with an expiration interval set in minutes. This will catch the case where flush () failed due to a SQLException and the record for this session must be removed from the card.

+3
source

How about using a FlushEventListener ?

+1
source

you can use post-commit-(update|delete|insert) for the listener type. it is called after the transaction

+1
source

All Articles