Hibernate Interceptors - Hibernate

What is Hibernate Interceptors?

As you contain learnt that in Hibernate, an entity will be created and persisted. Once the object has been transformed, it must be saved back to the database. This procedure continues until the next time the object is necessary, and it will be empty from the persistent store.

Thus an entity passes through different stages in its life cycle and Interceptor Interface provides methods which can be called at different stages to perform some optional tasks. These methods are call-backs from the session to the application, allowing the application to inspect and/or control properties of a persistent object before it is saved, updated, deleted or loaded. Following is the list of all the methods accessible within the Interceptor interface:

S.N. Method and Description
1 findDirty()
This method is be called when theflush()method is called on a Session object.
2 instantiate()
This method is called when a persisted class is instantiated.
3 isUnsaved()
This method is called when an object is passed to thesaveOrUpdate()method/
4 onDelete()
This method is called before an object is deleted.
5 onFlushDirty()
This method is called when Hibernate detects that an object is dirty (ie. have been changed) during a flush i.e. update operation.
6 onLoad()
This method is called before an object is initialized.
7 onSave()
This method is called before an object is saved.
8 postFlush()
This method is called after a flush has occurred and an object has been updated in memory.
9 preFlush()
This method is called before a flush.

Hibernate Interceptor gives us total manage over how an object will look to both the application and the database.

How to use Interceptors?

To accumulate an interceptor you can either perform Interceptor class straight or extend Empty Interceptor class. Following will be the easy steps to use Hibernate Interceptor functionality.

Create Interceptors:

We will extend Empty Interceptor in our instance where Interceptor's method will be called manually when Employee object is formed and updated. You can apply more methods as per your requests.

Create POJO Classes:

Currently let us transform a little bit our first instance where we used EMPLOYEE table and Employee class to play with:

Create Database Tables:

Second step would be creating tables in your database. There would be one table corresponding to each object you are willing to give persistence. Believe above matter need to be stored and retrieved into the following RDBMS table:

Create Mapping Configuration File:

This step is to generate a mapping file that educate Hibernate how to map the define class or classes to the database tables.

Create Application Class:

In the end, we will create our application class with the main () method to sprint the application. Here it should be noted that while creating session object we used our Interceptor class as an quarrel.

Compilation and Execution:

Here are the steps to accumulate and run the above mentioned application. Create sure you have set PATH and CLASSPATH correctly before proceeding for the compilation and implementation.

  • Create hibernate.cfg.xml configuration file as explained in configuration chapter.
  • Create Employee.hbm.xml mapping file as shown above.
  • Create Employee.java source file as shown above and compile it.
  • Create MyInterceptor.java source file as shown above and compile it.
  • Create ManageEmployee.java source file as shown above and compile it.
  • Execute Manage Employee binary to run the program.

You would get following consequence, and records would be formed in EMPLOYEE table.

If you prove your EMPLOYEE table, it should have following files:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Hibernate Topics