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