-->
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.  [ 1 post ] 
Author Message
 Post subject: Altering state inside an EventListener
PostPosted: Tue Aug 19, 2008 7:15 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Is this the right way to do it?

Code:
public class GekkoEventListener implements PreInsertEventListener {

    @Override
    public boolean onPreInsert(PreInsertEvent event) {
        handleSummaryAuditableEntity(
            event.getEntity(), event.getPersister(), event.getState(), true);
        return false;
    }

    private void handleSummaryAuditableEntity(
        Object entity,
        EntityPersister persister,
        Object[] state,
        boolean isPersist)
    {
        if (!(entity instanceof SummaryAuditable)) {
            return;
        }

        if (log.isTraceEnabled()) {
            log.trace("onPreInsert() called on SummaryAuditable: " + entity.getClass().getSimpleName());
        }

        User loggedInUser = // get currently logged in user
        Date now = new Date();

        if(isPersist){
            setPropertyValue(persister, state, "dateCreated", now);
            setPropertyValue(persister, state, "userCreated", loggedInUser);
        }
        setPropertyValue(persister, state, "dateUpdated", now);
        setPropertyValue(persister, state, "userUpdated", loggedInUser);
    }

    private void setPropertyValue(
        EntityPersister persister,
        Object[] state,
        String propName,
        Object propValue)
    {
        int propIndex =
            Arrays.asList(persister.getPropertyNames()).indexOf(propName);

        state[propIndex] = propValue;
    }


It seems to be working well enough for now (bootstrap data loading issues aside, see below).

Anyone got any experience with using the EventListener stuff this way? The book/manual only really talk about doing this stuff in the old interceptor way, I assume this is pretty much funcitonally identical?

Related to the structure given above (with the userCreated, userUpdated properties): is there any way I can get Hibernate to help me bootstrap the initial user into the system?

Our User object has a mandatory Title object (ie. non-null foreign key), and both of them are SummaryAuditable. So there's a circular dependency issue when inserting the "system" bootstrap user. We're using Hibernate to generate the schema, so we don't know the foreign key names, which means we can't (easily) just write a simple bootstrap load user script.

P.S. Hibernate 3.2

_________________
Cheers,
Shorn.


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

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.