Hibernate Interceptor Lesson



We will use following files
Entity.java
Entity.hbm.xml
hibernate.cfg.xml
MyInterceptor.java
HibernateEngine.java

Entity.java
public class Entity{
private int id;
String name;
String category;
}

Entity.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="javafiles.Entity" table="ENTITIES_DATA">
<id name="id" column="ID" >
<generator class="assigned" />
</id>
<property name="name" column="NAME" length="20"/>
<property name="category" column="CATEGORY" length="20"/>
</class>
</hibernate-mapping>

MyInterceptor.java
package javafiles;
import java.io.Serializable;
import java.util.Iterator;
import org.hibernate.CallbackException;
import org.hibernate.EmptyInterceptor;
import org.hibernate.EntityMode;
import org.hibernate.type.Type;

public class MyInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = 1L;

public MyInterceptor() {
System.out.println("MyInterceptor :-) MyInterceptor() called....");
}

public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
Object[] arg3, String[] arg4, Type[] arg5) {
System.out.println("MyInterceptor :-) findDirty() method called....");
return null;
}

public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)
throws CallbackException {
System.out.println("MyInterceptor :-) instantiate() method called....");
return null;
}
public void onDelete(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
System.out.println("MyInterceptor :-) onDelete() method called....");
}

public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
System.out.println("MyInterceptor :-) onLoad() method called....");
return false;
}

public boolean onSave(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
System.out.println("MyInterceptor :-) onSave() method called....");
return false;
}

public void postFlush(Iterator arg0) throws CallbackException {
System.out.println("MyInterceptor :-) postFlush() method called....");
}

public void preFlush(Iterator arg0) throws CallbackException {
System.out.println("MyInterceptor :-) preFlush() method called....");
}

}

HibernateEngine.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateEngine {
public static void main(String[] javaPlanet)
{
System.out.println("******************* START ******************");
System.out.println("HibernateEngine :-)HIBERNATE INTERCEPTORS LESSON....\n");
Configuration configurationObj = new Configuration();
configurationObj.configure("hibernate.cfg.xml");
SessionFactory sessionFactoryObj = configurationObj.buildSessionFactory();
Session sessionObj = sessionFactoryObj.openSession(new MyInterceptor());
Transaction transactionObj = sessionObj.beginTransaction();
Entity entityObj=new Entity();
entityObj.setId(101);
entityObj.setName("java");
entityObj.setCategory("Programming Language");

System.out.println("HibernateEngine :-) calling save() method....");
sessionObj.save(entityObj);

System.out.println("HibernateEngine :-) called save() method....");
System.out.println("HibernateEngine :-) calling delete() method....");
sessionObj.delete(entityObj);

System.out.println("HibernateEngine :-) called delete() method....");
System.out.println("HibernateEngine :-) calling flush() method....");
sessionObj.flush();

System.out.println("HibernateEngine :-) called flush() method....");
System.out.println("HibernateEngine :-)calling clear() method....");
sessionObj.clear();

System.out.println("HibernateEngine :-)called flush() method....");
System.out.println("HibernateEngine :-)calling commit() method....");
transactionObj.commit();

System.out.println("HibernateEngine :-)called flush() method....");
System.out.println("HibernateEngine :-)calling session close() method....");
sessionObj.close();

System.out.println("HibernateEngine :-)called session close() method....");
System.out.println("HibernateEngine :-)calling sessionFactory close() method....");

sessionFactoryObj.close();
System.out.println("HibernateEngine :-)called sessionFactory close() method....");

System.out.println("\n....SAVE-DELETE OPERATIONS SUCCESSFULLY DONE....");
System.out.println("....HIBERNATE INTERCEPTORS LESSON DONE....");
System.out.println("******************* END ******************");
}
}