I'm using some entity listener classes with my Empire-annotated beans to do validation & some other things, and in a couple of cases I throw exceptions from the listener methods e.g. when validation fails. A toy example:
public class RemoveProhibitor {
@PreRemove
public void prohibit(Object o) {
throw new PersistenceException( o.getClass().getName() + " instances may not be removed");
}
}
I would expect that this would in fact prevent the removal of o, but it doesn't, apparently because in EntityManagerImpl all exceptions thrown by the listener method are caught, logged, and not re-thrown:
try {
for (Method aListenerMethod : aListenerMethods) {
aListenerMethod.invoke(aListener, theObj);
}
}
catch (Exception e) {
LOGGER.error("There was an error during lifecycle notification for annotation: " +
theLifecycleAnnotation + " on object: " + theObj + ".", e.getCause());
}
Same deal for pre-persist & pre-update methods, I would expect that exceptions thrown in the listener method would cause the persist & merge operations to fail - although, I am not an expert in JPA so perhaps my expectation is not correct.
What do you think about adding the following second line to the catch block [after the LOGGER.error(...)] :