Contact Us
A 101-103 Siddhivinayak Towers, Off S.G. Highway, Ahmedabad, Gujarat 380051

Model Listener Hook in Liferay DXP 7.1

by Attune World Wide / /

Model Listeners are used for listening to the Liferay Model Events. Model Listeners implement the ModelListener interface. They are used to listen for persistence events on models and perform actions based on requirement either before or after creation of event.

Model listeners were designed to perform lightweight actions in response to a create, remove, or update attempt on an entity’s database table or a mapping table.

For example, if we want to perform some specific action after deleting a user, we can implement it using a model listener.

Requirements:Liferay 7 Dxp and knowledge of creating liferay hooks

You can create a model listener in a module by doing two simple things:

  • Implement ModelListener
  • Register the service in Liferay’s OSGi runtime

Step 1: Create a model Listener class that extends BaseModelListener<CustomEntity> to override listener methods from parent class. Here in CustomEntity you can define any entity of liferay as well as custom entity also. Here I have used User as an entity.

Step 2: Register the Model Listener Service

Register the service with Liferay’s OSGi runtime. If using Declarative Services, set service= ModelListener.class and immediate=true in the Component.

Step 3: Now implement listener methods and write your custom logic to perform actions. Here I have used onAfterRemove() and onBeforeRemove().

There are multiple methods of model listener used to perform action on model.

Listening for Persistence Events:

onAfterAddAssociation onBeforeAddAssociation
onAfterCreate onBeforeCreate
onAfterUpdate onBeforeUpdate
onAfterRemove onBeforeRemove
onAfterRemoveAssociation onBeforeRemoveAssociation

About Attune World Wide

What you can read next

Leave a Reply

Your email address will not be published. Required fields are marked *

10 − 9 =

Recent Posts

X