-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Custom persister
PostPosted: Wed Mar 09, 2005 11:21 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
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?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 1:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You want to extend BasicEntityPersister.

Quite a bit has changed in here though; most notably the addition of the Tuplizer stuff.

Have a look at SingleTableEntityPersister for guidance.

Have fun! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.