-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Validation using PreUpdateEventListener
PostPosted: Thu Mar 16, 2006 12:48 pm 
Newbie

Joined: Wed Mar 01, 2006 3:23 pm
Posts: 8
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.