In the interceptor scenario, what's the best way to throw an exception?
Code:
public class ValidationInterceptor extends EmptyInterceptor {
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types){
try{
//Validate work done here
ValidateWorker.validate(entity);
} catch (HibValEx hex){
//This is my own exception class which extends HibernateException
throw hex;
}
return false;
}
}
When I throw a validation exception I get this error:
Code:
16 [http-127.0.0.1-80-1] ERROR org.hibernate.AssertionFailure - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null id in reger.dao.hibernate.Groups entry (don't flush the Session after an exception occurs)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:48)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:150)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:106)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:978)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:135)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:113)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1602)
at org.apache.jsp.test.hibernate_002dassociations_log._jspService(org.apache.jsp.test.hibernate_002dassociations_log:390)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
To me it looks like Hibernate is still trying to save the entity after I've thrown the exception.
What's a best practice for implementing validation using an interceptor and throwing exceptions?
Thanks,
Joe