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.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on