Interceptors in Hybris
- 28th Jul 2018
- 4
- 123914
- Different types of interceptors in Hybris Init Defaults interceptor in hybris Interceptors in hybris Load interceptor in hybris Prepare interceptor in hybris Remove interceptor in hybris Validate interceptor in hybris when do we use interceptors in hybris ? why we need interceptors in hybris
We know that Models in Hybris are the representation of database tables and we often use models to save data in database and to load data from Database.
Please read all data modeling articles under overview-of-items-xml-file
These models we use in Hybris has its own lifecycle managed by ModelService.
There are several methods provided by ModelService to represent its lifecycle.
Example : Create, Save, Update, Load, Remove.
When we call any of these ModelService method, it just follows its Lifecycle and completes the execution.
Example : Save method save the model data in Database.
Why we need interceptors ?
The interceptors have the ability to interrupt the lifecycle of a model and execute some code and then allow the model to continue its lifecycle.
When we use interceptors ?
We need to use interceptors whenever we want to perform some action during the life cycle of a model.
Example : If we need to validate the model data before saving in database
We can see the main use of interceptors in some scenarios as mentioned below
1. Auditing the details while saving the model data
2. Validating the model data before saving
3. Restrict data from deleting or modifying based on some conditions
We can even modify the model before saving it using interceptors
Types of interceptors in Hybris
There are 5 main interceptors provided by Hybris
1) Init Defaults interceptor
2) Prepare interceptor
3) Validate interceptor
4) Load interceptor
5) Remove interceptor
Let’s understand these interceptors
1) Init Defaults interceptor
This interceptor is represented by an interface called “InitDefaultsInterceptor”
This is called when a model needs to be filled with its default values.
This will be called automatically when modelService.create method is called or when the modelService.initDefaults method is called.
We can use this interceptor to fill the model with additional default values, apart from the values defined in items.xml file
2) Prepare interceptor
This interceptor is represented by an interface called “PrepareInterceptor”
This is called before a model is saved to the database and before it is validated by Validate interceptors.
We can use this to add values to the model or modify existing ones before they are saved.
If any exception raised during execution, Model will not be saved
3) Validate interceptor
This interceptor is represented by an interface called “ValidateInterceptor”
This is called before a model is saved to the database after is been prepared by the Prepare interceptors.
We can use this Interceptors to validate values of the model and raise an InterceptorException if any values are not valid.
4) Load interceptor
This interceptor is represented by an interface called “LoadInterceptor”
This is called when we retrieve the model from the database.
We can use load interceptor if we want to change the values of the model after loading it.
If any exception raised during execution, Model will not be loaded
5) Remove interceptor
This interceptor is represented by an interface called “RemoveInterceptor”
This is called before a model is removed/deleted from the database.
We can use remove interceptor whenever we need to remove models that are related to the model but are not in the model context.
We can also use to prevent the removal/deletion of the model if it’s always required.
Note :
Each of the above interceptor addresses a particular step of the model life cycle.
When the life cycle of a model reaches a certain step, a corresponding interceptor will be executed automatically.
Pls Update the Tutorial
What is the purpose of PersistenceOperation?
Do not quite get it.
Hi karibasappa
Your blog is so helpful especially while preparing for interviews. Thankyou so much for that. Can you please post a tutorial on converters and populators
Hi Sir,
Could you plse provide the business use-case in real world application for all this interceptor with short description of example.