I am using some Hibernate event listeners which all look like this one:
Code:
public final class IntegrityGuardianPreUpdate implements PreUpdateEventListener {
private static final Class validatableObjectClass = ValidatableObject.class;
private final DefaultPreUpdateEventListener defaultListener = new DefaultPreUpdateEventListener();
private IntegrityGuardianPreUpdate() {
}
public static final IntegrityGuardianPreUpdate INSTANCE = new IntegrityGuardianPreUpdate();
@SuppressWarnings("unused")
private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
public boolean onPreUpdate(final PreUpdateEvent event) throws HibernateException {
final Object toPreUpdate = event.getEntity();
if (validatableObjectClass.isInstance(toPreUpdate)) {
final Session s = HibernateUtil.currentSession();
s.setFlushMode(FlushMode.COMMIT);
final List<Problem> theProblems;
try {
theProblems = ((ValidatableObject) toPreUpdate)
.validate();
} catch (final HibernateException e) {
if (Loggers.DATA_VALIDATION.isEnabledFor(Level.FATAL)) {
Loggers.DATA_VALIDATION.fatal("Error in validator.",e);
}
throw e;
}
s.setFlushMode(FlushMode.AUTO);
if (theProblems.size()>0)
throw new ValidationException(theProblems);
}
return defaultListener.onPreUpdate(event);
}
}
I am getting lots of AssertionFailure: Collection was not processed by flush. I suspect that this has something to do with me changing FlushMode to commit during validation (but as I need to validate against the DB's state from before I save, I can't let the session autoflush).
Am I right in my assumption, and if yes, is there a way out of this?