3.4.8. EntityPersistingEvent

EntityPersistingEvent is a Spring’s ApplicationEvent which is sent by the framework on the middle tier before a new instance is saved to the database. At the moment of sending the event, an active transaction exists.

EntityPersistingEvent can be used to initialize entity attributes before creating it in the database:

package com.company.demo.core;

import com.company.demo.entity.Customer;
import com.haulmont.cuba.core.app.events.EntityPersistingEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component("demo_CustomerChangedListener")
public class CustomerChangedListener {

    @EventListener
    void beforePersist(EntityPersistingEvent<Customer> event) {
        Customer customer = event.getEntity();
        customer.setCode(obtainNewCustomerCode(customer));
    }

    // ...
}