In Hibernate 2.x, I have the following Customer persister. I added few methods in here to get some meta data information (specifcally the
database column names).
Code:
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.engine.SessionFactoryImplementor;
import net.sf.hibernate.mapping.PersistentClass;
import net.sf.hibernate.persister.EntityPersister;
/**
* Custome Persister that exposes public methods to get the actual column name
* ,of the property/domain class this persister is associated with, using the
* "protected" method EntityPersister.getActualPropertyColumnNames.
*
* @author Balaji Muthuvarathan
* @version $Revision: 1.1 $ $Date: 2004/06/15 13:52:32 $
*
*/
public class CustomPersister extends EntityPersister {
/**
* @param model
* @param factory
* @throws net.sf.hibernate.HibernateException
*/
public CustomPersister(PersistentClass model, SessionFactoryImplementor factory)
throws HibernateException {
super(model, factory);
}
/**
* Get the String Array and return the first string (that contains
* the column name of the property refered by the index i).
* @param i
* @return
*/
public String getColumnName(int i) {
return (super.getActualPropertyColumnNames(i))[0];
}
/**
* For Component Types, return all property column names
* @param i
* @return
*/
public String[] getColumnNames(int i) {
return super.getActualPropertyColumnNames(i);
}
}
I am trying upgrade to Hibernate 3.0 and am not sure where/how I could accomplish this (i.e what is the class the customer persister should extend , I do get the columnames or is there a better way to do this?). I use this info. to generate feild level detailed audit logs that would show the previous and current values on updates (along with the table name/ id/ column name etc.)
Just in case if you are wondering why I do this
the previosState in the Interceptor.onFlushDirty is always null for detached objects ( even with the select-before-update set). I hate this by the way. I read in a differed post, the solution suggested by the hibernate team was to use a long session , but my app is completely stateless so that is out of the question.
And, just in case if you are wondering how I use this,
(Obviously the persister is specified in the mapping file). Then
Map m = sessionFactory.getAllClassMetadata();
CustomerPersister sp = m.get(someClass)
String tableName = sp.getTableName();
Class domainClass = sp.getType().getReturnedClass();
String[] props = sp.getPropertyNames();
Type[] types = sp.getPropertyTypes();
//At this point I also have the updated detached object and the
//object representing the current DB state that I loaded in a
//different read-only session. ie 2 complete detached object graphs, one updated by the front end and the other representing the current DB state
//loop thro the props, use Java reflection to get the values of each and every property values from the 2 object graphs, compare them and
generate the detailed audit logs. I also have code to handle special properties like collections and associations (using the type).
fun huh?