Hibernate version: 3.1.2
I'm trying to implement a validation framework using PreUpdateEventListener and PreInsertEventListener. I want to have a void validate() method on each of my entity POJOs. When my custom validator throws an exception I convert it to a hibernate ValidationFailure and throw it. I then get an "AssertionFailure: null id" when I next try to read the object. It would appear that I'm not throwing the validation error properly.
I have tried to make my POJOs implement Validatable but I get a similar error when hibernate manages the lifecycle and calls validate() itself.
Question:
How should I throw an error from inside a PreUpdateEventListener such that I do not break my hibernate session?
RegerPreUpdateEventListener:
Code:
public class RegerPreUpdateEventListener implements PreUpdateEventListener {
public boolean onPreUpdate(PreUpdateEvent event){
try{
RegerEntity entity = (RegerEntity)event.getEntity();
//My custom validation code
entity.validate();
} catch (reger.core.ValidationException vex){
//Convert my exception into one that hibernate understands
org.hibernate.classic.ValidationFailure vf = new org.hibernate.classic.ValidationFailure(vex.getErrorsAsSingleString());
throw vf;
}
return false;
}
}
Code between sessionFactory.openSession() and session.close():Code:
try{
Session hsession = getSession();
hsession.beginTransaction();
//obj is my groups object that I'm trying to update
//It was in an unattached state prior to this
hsession.saveOrUpdate(obj);
hsession.getTransaction().commit();
hsession.close();
} catch (org.hibernate.classic.ValidationFailure vf){
ValidationException ve = new ValidationException();
ve.addValidationError(vf.getMessage());
throw ve;
}
Full stack trace of any exception that occurs:Code:
2456874 [http-127.0.0.1-80-3] 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.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at reger.hibernate.HibernateUtil.save(HibernateUtil.java:121)
at org.apache.jsp.test.hibernate_002dassociations_log._jspService(org.apache.jsp.test.hibernate_002dassociations_log:222)
Name and version of the database you are using: MySQL 5
Thanks,
Joe