4.1.4. Using Entity Listeners
Entity listeners allow you to execute your business logic each time an entity is added, updated or removed from the database. For example, we could recalculate the discount for a customer each time an order for this customer is changed.
An entity listener stub can be easily created using Studio:
Switch to the Middleware section and click New > Entity listener.
Change the class name to
OrderEntityListenerand select checkboxes for
Orderentity in the Entity type field.
Click OK or Apply and open the listener class in your IDE.
See an example implementation in the demo application:
If you open the Logic in Entity Listeners screen of the demo application, you will see two tables: orders and customers. Create, edit or remove an order, then refresh the customers table, and you will see that the discount of the corresponding customer is changed.