Hibernate Event Listeners

The Axelor Open Platform provides a mechanism for registering custom listeners for various Hibernate events, such as pre-insert, pre-update, post-insert, etc.

Usage

To add a custom Hibernate event listener, you need to follow these steps:

  1. Create a listener class.

  2. Create a configurator class that implements HibernateListenerConfigurator.

  3. Bind the configurator in your AxelorModule.

Create a Listener

Implement one of Hibernate’s event listener interfaces. For example, to execute logic before an entity is inserted, you would implement org.hibernate.event.spi.PreInsertEventListener.

public class MyPreInsertEventListener implements PreInsertEventListener {

  private static final Logger log = LoggerFactory.getLogger(MyPreInsertEventListener.class);

  @Override
  public boolean onPreInsert(PreInsertEvent event) {
    if (event.getEntity() instanceof Product) {
      log.info("A new Product is about to be inserted: {}", event.getEntity());
    }

    // Return false to allow the insertion to proceed.
    return false;
  }
}

Implement a Configurator

Create a class that implements com.axelor.db.audit.HibernateListenerConfigurator. This class is responsible for registering your listeners with Hibernate’s EventListenerRegistry. You can inject your listener instances directly into the configurator.

public class MyHibernateListenerConfigurator implements HibernateListenerConfigurator {

  private final MyPreInsertEventListener myPreInsertEventListener;
  // Possibly declare more listeners...

  @Inject
  public MyHibernateListenerConfigurator(MyPreInsertEventListener myPreInsertEventListener) {
    this.myPreInsertEventListener = myPreInsertEventListener;
  }

  @Override
  public void registerListeners(EventListenerRegistry registry) {
    registry.appendListeners(EventType.PRE_INSERT, myPreInsertEventListener);
    // Possibly register more listeners...
  }
}

Bind a Configurator

Finally, bind your configurator in your module’s configure() method using helper method addHibernateListenerConfigurator() that uses Guice’s Multibinder to be able to bind multiple configurators.

public class MyModule extends AxelorModule {

  @Override
  protected void configure() {
    // Other configurations...

    // Bind the configurator.
    // You can add multiple configurators from different modules.
    addHibernateListenerConfigurator(MyHibernateListenerConfigurator.class);
  }
}

Supported Event Types

You can register listeners for any event type defined in the org.hibernate.event.spi.EventType enum. Common event types include:

  • PRE_INSERT

  • PRE_UPDATE

  • PRE_DELETE

  • POST_INSERT

  • POST_UPDATE

  • POST_DELETE

  • POST_LOAD

  • And others…​

You can register listeners for any of these event types using the EventListenerRegistry provided in the registerListeners method.

Performance Considerations

Hibernate event listeners should be used sparingly because of performance concerns. Each listener adds overhead to database operations, and excessive or poorly optimized listeners can significantly impact application performance. Consider the necessity of each listener and optimize their logic to minimize processing time.