Feed on
 Posts
 Comments
Java Beans dot Asia

Just a few simple tutorials …

Its quite common when you create an application, there is a need to create an audit trail on the application level where all entity insert, update and delete events are logged.

In this post, I would like to describe a simple approach that can help you to avoid littering with unnecessary statements in your application code. The solution is to register a class as a listener on Hibernate events. Once class is triggered, you will be able to write audit information to a database or log file.

The following shows a Hibernate event interceptor class that is triggered when persistent entity is inserted, deleted or updated.

public class HibernateEventInterceptor	implements	PostInsertEventListener,
							PostUpdateEventListener,
							PostDeleteEventListener,
							Initializable {

	public HibernateEventInterceptor() {

	}

	public void initialize(Configuration cfg) {

	}

	public void onPostInsert(PostInsertEvent event) {
		String entityName = event.getPersister().getEntityName();
		System.out.println("Inserted entity: " + entityName);
	}

	public void onPostUpdate(PostUpdateEvent event) {
		String entityName = event.getPersister().getEntityName();
		System.out.println("Updated entity: " + entityName);
	}

	public void onPostDelete(PostDeleteEvent event) {
		String entityName = event.getPersister().getEntityName();
		System.out.println("Deleted entity: " + entityName);
	}
}

The following shows extra configuration that must be added to persistence.xml, in order for the interceptor class to be triggered:

<persistence>
	<persistence-unit name="org.example.demo">
	<jta-data-source>java:/test</jta-data-source>
		<properties>
			.
			.
			.
		<property name="hibernate.ejb.event.post-insert" value="org.example.demo.HibernateEventInterceptor"/>
		<property name="hibernate.ejb.event.post-update" value="org.example.demo.HibernateEventInterceptor"/>
		<property name="hibernate.ejb.event.post-delete" value="org.example.demo.HibernateEventInterceptor"/>
		</properties>
	</persistence-unit>
</persistence>
GD Star Rating
loading...

Related posts:

  1. Bitwise Operation In Hibernate 3
    Hi all… i encountered a small problem in doing bitwise operations with hibernate. Until now, HIbernate 2 HQL parser has supported bitwise operations....
  2. Serialize POJO to XML
    Today my colleague, Chandana was trying to convert POJO to an XML file. In our company we are working with dom4j library, but...
  3. Web Applications with AJAX, Servlets and JSON (Chinese Version)
    在这篇文章中,我想说明如何的JSON(JavaScript对象 符号)和Java Servlet可以同时使用在一个小的AJAX((Asynchronous JavaScript and XML)的应用。 为了作简要介绍那些谁不熟悉使用JSON密切。JSON是一种表示数据,这使得工作轻量级语法 它比更愉快,使AJAX应用程序的XML 快。此外,当使用JSON工作,没有任何一个XML需要 解析。...
  4. Using Template to Deploy a JBoss Queue
    Currently I am involved in a project, where I have to use Velocity template engine to deploy queues and message-driven beans to JBoss....
  5. Hibernate – How To Map Two Collections of The Same Type in The Same Entity
    Recently during development, I encountered a situation where I had to map two collections in the same entity, having collections and the entity...