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