-->
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: Interceptor.onFlushDirty - Getting only changed properties
PostPosted: Thu Oct 07, 2010 11:32 am 
Newbie

Joined: Thu Oct 07, 2010 11:19 am
Posts: 6
Hi all,
Regarding the onFlushDirty method of the Interceptor:
Code:
public boolean onFlushDirty(BaseEntity entity, Serializable id, Object[] currentState,
            Object[] previousState, String[] propertyNames)

I want to use an interceptor for auditing purposes.I'm expecting to get only the names of the dirty properties at the propertyNames parameter but I'm getting an array of the properties names contained at the saved entity.
Is there a way to get just the dirty properties?


Top
 Profile  
 
 Post subject: Re: Interceptor.onFlushDirty - Getting only changed properties
PostPosted: Thu Oct 14, 2010 3:44 am 
Newbie

Joined: Thu Oct 07, 2010 11:19 am
Posts: 6
In order to get the dirty properties to the onFlashDirty, perform the next steps:
1) Extend the DefaultFlushEntityEventListener as follows:

Code:
public class FlushEntityEventListenerDelegator<T> extends DefaultFlushEntityEventListener {

    @Override
    protected boolean handleInterception(FlushEntityEvent event) {
        SessionImplementor session = event.getSession();
        EntityEntry entry = event.getEntityEntry();
        EntityPersister persister = entry.getPersister();
        Object entity = event.getEntity();

        // give the Interceptor a chance to modify property values
        final Object[] values = event.getPropertyValues();
        final boolean intercepted = invokeInterceptor(session, event);

        // now we might need to recalculate the dirtyProperties array
        if (intercepted && event.isDirtyCheckPossible() && !event.isDirtyCheckHandledByInterceptor()) {
            int[] dirtyProperties;
            if (event.hasDatabaseSnapshot()) {
                dirtyProperties = persister
                        .findModified(event.getDatabaseSnapshot(), values, entity, session);
            } else {
                dirtyProperties = persister.findDirty(values, entry.getLoadedState(), entity, session);
            }
            event.setDirtyProperties(dirtyProperties);
        }

        return intercepted;
    }

    /**
     * This method will invoke the onFlushDirty with parameters
     * containing the dirty data.
     *
     * @param session
     * @param event
     * @return
     */
    protected boolean invokeInterceptor(SessionImplementor session, FlushEntityEvent event) {
        if (event.getDirtyProperties() == null) {
            return false;
        }
        EntityEntry entry = event.getEntityEntry();
        EntityPersister persister = entry.getPersister();
        Object entity = event.getEntity();
        final Object[] values = event.getPropertyValues();

        // Convert the values, properties names (old and new values)
        // and types to arrays that contains the dirty ones.
        int[] dirtyPropertiesIndexes = event.getDirtyProperties();
        int dirtyPropertiesLength = dirtyPropertiesIndexes.length;
        String[] dirtyPropertiesNames = new String[dirtyPropertiesLength];
        Object[] dirtyPropertiesValues = new Object[dirtyPropertiesLength];
        Object[] loadedPropertiesValues = new Object[dirtyPropertiesLength];
        Type[] dirtyPropertiesTypes = new Type[dirtyPropertiesLength];

        for (int i = 0; i < dirtyPropertiesLength; i++) {
            int dirtyPropertyIndex = dirtyPropertiesIndexes[i];
            dirtyPropertiesNames[i] = persister.getPropertyNames()[dirtyPropertyIndex];
            dirtyPropertiesValues[i] = values[dirtyPropertyIndex];
            dirtyPropertiesTypes[i] = persister.getPropertyTypes()[dirtyPropertyIndex];
            loadedPropertiesValues[i] = entry.getLoadedState()[dirtyPropertyIndex];
        }

        // Invoke the intercepter.onFlushDirty method.
        return session
                .getInterceptor()
                .onFlushDirty(entity, entry.getId(), dirtyPropertiesValues, loadedPropertiesValues, dirtyPropertiesNames, dirtyPropertiesTypes);
    }

    /**
     * This method was replaced with the invokeInterceptor(SessionImplementor session, FlushEntityEvent event)
     * method.
     *
     * @deprecated
     */
    @Deprecated
    @Override
    protected boolean invokeInterceptor(SessionImplementor session, Object entity, EntityEntry entry,
            final Object[] values, EntityPersister persister) {
        throw new UnsupportedOperationException(
                "Since this method was replaced with another one it was not suppose to be invoked.");
    }
}


2) Register the above listener to the hibernate session by performing the next steps:
    1) org.hibernate.cfg.Configuration configuration
    .getEventListeners()
    .setFlushEntityEventListeners(new FlushEntityEventListenerDelegator[] { new FlushEntityEventListenerDelegator<T>() });

    2) org.hibernate.SessionFactory hibernateSessionFactory = configuration.buildSessionFactory();


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.