-->
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.  [ 5 posts ] 
Author Message
 Post subject: Interceptor documentation example
PostPosted: Wed Aug 31, 2005 3:37 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
Hi,

I just finished the implementation of my Interceptor.
I based it on the documentation example and on the EmptyInterceptor.
Everything works fine except that I can't see the amount of object created.

The weird thing is that I can never have a log with 'Creations:' being different from 0. Whereas it works fine with updates:
when updating, preFlush() is called before onFlushDirty(), which is fine.

The reason for the failure is that the preFlush() method is called after onSave() when creating an object, reseting the local counter previously increased.

I can't figure out why, and according to the doc implementation, it's not an expected behavior.

Anyone already encountered the problem?

Btw, I don't really understand how to implement the getEntity() method.
What is the purpose of this one? the doc says:
Quote:
Get a fully loaded entity instance that is cached externally

Are we speaking about the 2nd level cache? Shall I use this method to load object from the DB?


the Interceptor's code:
Code:
/**
* @version Aug 29, 2005 - 7:22:53 PM
*/
public final class BoInterceptor implements Interceptor, Serializable {

    private Date dateMutualisation;
    private int updates;
    private int creates;
    private int deletes;

    /**
     * The <code>Log</code> instance for this package.
     */
    private Log log = LogFactory.getLog(this.getClass());

    /**
     * @param sessionFactory The sessionFactory to set.
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        Session session = sessionFactory.openSession();
        DateComptable dateComptable = (DateComptable) session.get(DateComptable.class, "DATE MUTUALISATION");
        this.dateMutualisation = dateComptable.getDateComptableDate();
        session.close();
    }

    public void onDelete(Object entity,
                         Serializable id,
                         Object[] state,
                         String[] propertyNames,
                         Type[] types) {
        this.deletes++;
    }

    public boolean onFlushDirty(Object entity,
                                Serializable id,
                                Object[] currentState,
                                Object[] previousState,
                                String[] propertyNames,
                                Type[] types) {
        this.updates++;
        boolean isModified = false;
        for (int i = 0; i < types.length; i++) {
            if ((types[i] instanceof StringType) && "".equals(currentState[i])) {
                currentState[i] = null;
                isModified = true;
                log.debug("Empty attribute '" + propertyNames[i] + "', replaced by 'null'");
            } else {
                if (propertyNames[i].endsWith("DateMaj") && (new Date()).after(this.dateMutualisation)) {
                    currentState[i] = this.dateMutualisation;
                    isModified = true;
                    log.debug("Updated Objet with Mutualisation Date :" + entity.getClass().getName() + " Date Mutualisation : " + this.dateMutualisation);
                }
            }
        }
        return isModified;
    }

    public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        return false;
    }

    public boolean onSave(Object entity,
                          Serializable id,
                          Object[] state,
                          String[] propertyNames,
                          Type[] types) {
        boolean isModified = false;
        this.creates++;
        for (int i = 0; i < types.length; i++) {
            if ((types[i] instanceof StringType) && "".equals(state[i])) {
                state[i] = null;
                isModified = true;
                log.debug("Empty attribute '" + propertyNames[i] + "', replaced by 'null'");
            } else {
                if ((propertyNames[i].endsWith("DateCreation") || propertyNames[i].endsWith("DateMaj")) && (new Date()).after(this.dateMutualisation)) {
                    state[i] = this.dateMutualisation;
                    isModified = true;
                    log.debug("Created Objet with Mutualisation Date :" + entity.getClass().getName() + " Date Mutualisation : " + this.dateMutualisation);
                }
            }
        }
        return isModified;
    }

    public void postFlush(Iterator entities) {
        log.debug("Creations: " + this.creates + ", Updates: " + this.updates + ", Deletes: " + this.deletes);
    }

    public void preFlush(Iterator entities) {
        this.updates = 0;
        this.creates = 0;
        this.deletes = 0;
    }

    public Boolean isTransient(Object entity) {
        return null;
    }

    public Object instantiate(String entityName, EntityMode entityMode, Serializable id) {
        return null;
    }

    public int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
        return null;
    }

    public String getEntityName(Object object) {
        return null;
    }

    public Object getEntity(String entityName, Serializable id) {
        return null;
    }

    public void afterTransactionBegin(Transaction tx) {
    }

    public void afterTransactionCompletion(Transaction tx) {
    }

    public void beforeTransactionCompletion(Transaction tx) {
    }

}



Hibernate version: 3.0.5

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: MSSQL Server

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:
[/quote]

_________________
/nodje


Last edited by nodje on Tue Jun 03, 2008 5:39 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 5:23 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
I have the same behavior for onDelete(): preFlush is called after onDelete().
That's probably a normal behavior, but then the doc example doesn't work.

Has anyone seen a more detailed documentation about Interceptor method and their roles?

cheers,

_________________
/nodje


Top
 Profile  
 
 Post subject: Interceptor
PostPosted: Thu Sep 01, 2005 5:32 am 
Beginner
Beginner

Joined: Tue Jul 19, 2005 4:03 am
Posts: 34
Location: Aberdeen, UK
We based ours implementation of the Interceptor on the example in Hibernate In Action, chapter 8.3.2 Audit Logging.

Otherwise there is an example at http://www.hibernate.org/195.html

Personally I found the Interceptors behaviour quite magically!!

cheers


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 6:37 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
hey, thanks for the answer.
My interceptor works fine as well, pretty amazing stuff.

I just want to get an in depth understanding of it.
And especially understand why preFlush() is called after onSave() or onDelete()...

cheers,

_________________
/nodje


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 10:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ah, good point, the example in the Hibernate refdoc is broken.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.