-->
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: Saving inside a PostUpdateListener
PostPosted: Thu Sep 29, 2005 9:13 pm 
Newbie

Joined: Thu Sep 29, 2005 9:00 pm
Posts: 4
Hibernate version: 3.0.5

Name and version of the database you are using: Mysql


I have been finding it hard to find examples/documentation on what is allowed/not allowed, etc within the Event Listeners. I have a PostUpdateListener that comapres oldState and the currentState and attepts to save off any changes (the ChangeGroup/Items are Hibernated objects themselves) Is this allowed?

I am running into a case where saving Person triggers my listener to make a ChangeGroup/Items (before you ask only if the entity implements Loggable will it try to save the change, and no, ChangeGroup/Items does not implement Loggable) and saving that ChangeGroup/Items seems to trigger person to be saved all over again. It recursively calls PostUpdateListener again and again until a stack overflow is thrown.

My listener is....
Code:
public class ChangeListener implements PostUpdateEventListener
{
   private static final ChangeGroupStore store = new ChangeGroupStore();

   public void onPostUpdate(PostUpdateEvent event) throws HibernateException
   {
      Object entity = event.getEntity();
      if(isLoggable(entity))
      {
         ChangeGroup change = createChangeGroup(entity, event.getId());
         change.setType(ChangeGroupType.UPDATE);

         EntityPersister persister = event.getPersister();
         String[] propertyNames = persister.getPropertyNames();
         Object[] oldValues = event.getOldState();
         Object[] currentValues = event.getState();
         for (int i = 0; i < propertyNames.length; i++)
            if (!DomainUtils.equals(oldValues[i], currentValues[i]))
               change.addChangeItem(new ChangeItem(propertyNames[i], oldValues[i].toString(), currentValues[i].toString()));

         store.save(change);
      }
   }

   private boolean isLoggable(Object o)
   {
      System.out.println("Should we log " + o.getClass());
      return o instanceof Loggable;
   }

   private ChangeGroup createChangeGroup(Object entity, Serializable id)
   {
      ChangeGroup change = new ChangeGroup();
      change.setEntityClass(entity.getClass());
      change.setEntityId(id);

      Loggable changedObject = (Loggable)entity;
      change.setCategoryClass(changedObject.getCategoryClass());
      change.setCategoryId(changedObject.getCategoryId());

      String user = UserContext.getUser();
      change.setUser(user == null ? "Unknown User" : user);
      return change;
   }
}


I think I'm doing something wrong, but an not sure what it could be...

Thanks,
Frank


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 1:57 pm 
Newbie

Joined: Thu Sep 29, 2005 9:00 pm
Posts: 4
I think I got it, I would love it if this was documented somewhere...

I had to create a new Session and save the ChangeGroup/Items on that. If I saved them on the currently existing Session it created some endless recursion, for some reason the save of the ChangeGroup make the Session think that the Person I was saving was always dirty, or something like that.

Can anyone clarify it just a bit? Can we get any additional paragraphs added to the Event section to explain things like this?

Looking forward to hearing more details,
Frank

PS Once I get something working a bit better I can post a page to the Wiki.


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.