Post-commit-* listeners not firing

Hello everyone,

We have been successfully using Hibernate for several years. We use a technology stack consisting of Spring and Hibernate, along with Envers auditing. Some key aspects of the solution are based on EntityListeners. We register them as follows:

@PostConstruct
void selfRegister()
{
	if (sessionFactory instanceof final SessionFactoryImplementor sessionFactoryImplementor)
	{
		// As you might expect, an EventListenerRegistry is the place with which event listeners are registered
		// It is a service so we look it up using the service registry
		final EventEngine eventEngine = sessionFactoryImplementor.getEventEngine();
		final EventListenerRegistry eventListenerRegistry = eventEngine.getListenerRegistry();
			
		// add the listener to the end of the listener chain
		eventListenerRegistry.appendListeners(EventType.PERSIST, EntityListener.this);
		eventListenerRegistry.appendListeners(EventType.MERGE, EntityListener.this);
		eventListenerRegistry.appendListeners(EventType.PRE_INSERT, EntityListener.this);
        eventListenerRegistry.appendListeners(EventType.POST_COMMIT_INSERT, EntityListener.this);
		eventListenerRegistry.appendListeners(EventType.POST_COMMIT_UPDATE, EntityListener.this);
		eventListenerRegistry.appendListeners(EventType.POST_COMMIT_DELETE, EntityListener.this);
		eventListenerRegistry.appendListeners(EventType.POST_COLLECTION_UPDATE, EntityListener.this);
	}
	else
	{
		throw new IllegalStateException("Unable to register entity listeners!");
	}
}

We are currently working on updating hibernate from version 5 to version 7 (7.1.0.final in detail) using JDK 21. We have not changed the above code section. However, we can see that the listeners for the following events are no longer being called.

  • POST_COMMIT_INSERT
  • POST_COMMIT_UPDATE
  • POST_COMMIT_DELETE
  • POST_COLLECTION_UPDATE

We’re using annotation-driven transactions with Spring’s @Transactional annotations. Do we need to change any configuration or explicitly enable these events?

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<qualifier value="txmUserDB"></qualifier>
		<property name="sessionFactory" ref="sessionFactoryUser" />
	</bean>

	<!-- enable the configuration of transactional behavior based on annotations -->
  	<tx:annotation-driven transaction-manager="transactionManager"/>

Kind regards

Sebastian

Okay, I guess I didn’t dig deep enough. Anyway, I can answer my own question here in case anyone has a similar problem.

In Hibernate 5, the method

org.hibernate.event.spi.PostActionEventListener.requiresPostCommitHandling(EntityPersister)

internally called the abstract (deprecated) method

org.hibernate.event.spi.PostActionEventListener.requiresPostCommitHanding(EntityPersister)

This was implemented by my listener implementations and always returned true.
With Hibernate 7, this method was finally removed, and the return value of the former method was changed to false. This resulted in all POST-COMMIT listeners no longer being called in my software.
The solution is to implement the method org.hibernate.event.spi.PostActionEventListener.requiresPostCommitHandling(EntityPersister) (this uses a default implementation at the interface level) and return true here like this:

eventListenerRegistry.appendListeners(EventType.POST_COMMIT_INSERT, new PostCommitInsertEventListener()
{
	@Override
	public void onPostInsert(final PostInsertEvent pEvent)
	{
		// TODO: implement your magic here
	}

	@Override
	public void onPostInsertCommitFailed(final PostInsertEvent pEvent)
	{
		// TODO: handle failed commit if necessary
	}
	
	@Override
	public boolean requiresPostCommitHandling(final EntityPersister pPersister)
	{
        // indicate the action queue that this listener requires post-commit handling
		return true;
	}
});

Have a nice day!