-->
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.  [ 4 posts ] 
Author Message
 Post subject: saveOrUpdateEventListener of interceptor for validation?
PostPosted: Fri Sep 23, 2005 2:18 am 
Newbie

Joined: Thu Mar 17, 2005 7:06 am
Posts: 4
Hi,

I want to call an isValid() method on every object hibernate is going to save into the database.

Now i have two options:
- Extend the DefaultSaveOrUpdateEventListener and implement the onSaveOrUpdate() method (and thus calling object.isValid() ).
- Create an interceptor and implement onFlushDirty() and onSave() ( and call the isValid() in these two methods).

Which of the two options guarantees that each and every object, that is or going to be persisted, is validated?
Can anyone give me advice on the best option?
[/list]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 2:38 am 
Newbie

Joined: Thu Mar 17, 2005 7:06 am
Posts: 4
anyone? ;)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 3:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
both will probably work, but I would say Interceptor is the simplest of the two.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 12:41 pm 
Newbie

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


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.