Sunday, February 21, 2010

Hibernate interceptor example – audit log

Hibernate has a powerful feature called ‘interceptor‘ to intercept or hook different kind of Hibernate events, like database CRUD operation. In this article, i will demonstrate how to implement an application audit log feature by using Hibernate interceptor, it will log all the Hibernate save, update or delete operations into a database table named ‘auditlog‘.
Hibernate interceptor example – audit log
1. Create a table

Create a table called ‘auditlog’ to store all the application audited records.

DROP TABLE IF EXISTS `mkyong`.`auditlog`;
CREATE TABLE  `mkyong`.`auditlog` (
 `AUDIT_LOG_ID` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
 `ACTION` varchar(100) NOT NULL,
 `DETAIL` text NOT NULL,
 `CREATED_DATE` date NOT NULL,
 `ENTITY_ID` bigint(20) UNSIGNED NOT NULL,
 `ENTITY_NAME` varchar(255) NOT NULL,
 PRIMARY KEY (`AUDIT_LOG_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

2. Create a marker interface

Read more: Mkyong Dot Com

Posted via email from jasper22's posterous