alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (StatefulInterceptor.java)

This example Hibernate source code file (StatefulInterceptor.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

arraylist, emptyinterceptor, io, iterator, log, log, object, object, serializable, serializable, session, statefulinterceptor, string, string, type, util

The Hibernate StatefulInterceptor.java source code

//$Id: StatefulInterceptor.java 7701 2005-07-30 05:07:01Z oneovthafew $
package org.hibernate.test.interceptor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.EmptyInterceptor;
import org.hibernate.Session;
import org.hibernate.type.Type;

public class StatefulInterceptor extends EmptyInterceptor {
	
	private Session session;

	private List list = new ArrayList();
	
	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
		if ( !(entity instanceof Log) ) {
			list.add( new Log( "insert", (String) id, entity.getClass().getName() ) );
		}
		return false;
	}

	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
		if ( !(entity instanceof Log) ) {
			list.add( new Log( "update", (String) id, entity.getClass().getName() ) );
		}
		return false;
	}

	public void postFlush(Iterator entities) {
		if ( list.size()>0 ) {
			for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
				session.persist( iter.next() );	
			}
			list.clear();
			session.flush();
		}
	}
	
	public void setSession(Session s) {
		session = s;
	}

}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate StatefulInterceptor.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.