-->
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.  [ 5 posts ] 
Author Message
 Post subject: Custom validator stuck in continuous loop
PostPosted: Fri Aug 15, 2008 11:28 am 
Newbie

Joined: Fri Aug 15, 2008 11:18 am
Posts: 6
Location: South Carolina
I wrote a validator to check for duplicate usernames. I thought validation was only called on a store or update operation.
Code:
   public boolean isValid(Object value)
   {
      String username = (String)value;
      
      UserAccountManager manager =
         (UserAccountManager)Component.getInstance("userAccountManager");
      
      //no change to username
      if( username.equals(manager.getPreviousUsername()) )
      {
         return true;
      }
   
      UserAccount account = manager.findUserAccount(username);
      
      if(account == null)
      {
         return true;
      }
      
      return false;
   }


I wouldn't think that this would cause a store or update operation.

Code:
   public UserAccount findUserAccount( String searchName )
   {
      UserAccount account;
      
      try{
      account = (UserAccount)em.createQuery
         ("select a from UserAccount a where a.username = :name")
         .setParameter("name", searchName)
         .getSingleResult();
      } catch (NoResultException ex) {
         account = null;
      }
      return account;
   }



But the Exception stack shows a continual looping. Looks like a call to auto flush might be the cause. Am I doing something to cause this? Shouldn't I be able to do a read operation within a validator. I think I can do a workaround but I would like to understand why this is happening.

at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:341)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:307)
at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:134)
at org.hibernate.validator.event.ValidateEventListener.onPreUpdate(ValidateEventListener.java:172)
at org.hibernate.action.EntityUpdateAction.preUpdate(EntityUpdateAction.java:217)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:65)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
at org.domain.BeReadyCounty.action.UserAccountManagerBean.findUserAccount(UserAccountManagerBean.java:104)
at org.domain.BeReadyCounty.model.UsernameValidator.isValid(UsernameValidator.java:33)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:341)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:307)
at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:134)
at org.hibernate.validator.event.ValidateEventListener.onPreUpdate(ValidateEventListener.java:172)
at org.hibernate.action.EntityUpdateAction.preUpdate(EntityUpdateAction.java:217)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:65)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
at org.domain.BeReadyCounty.action.UserAccountManagerBean.findUserAccount(UserAccountManagerBean.java:104)
at org.domain.BeReadyCounty.model.UsernameValidator.isValid(UsernameValidator.java:33)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:341)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:307)
at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:134)
at org.hibernate.validator.event.ValidateEventListener.onPreUpdate(ValidateEventListener.java:172)
at org.hibernate.action.EntityUpdateAction.preUpdate(EntityUpdateAction.java:217)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:65)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
at org.domain.BeReadyCounty.action.UserAccountManagerBean.findUserAccount(UserAccountManagerBean.java:104)
at org.domain.BeReadyCounty.model.UsernameValidator.isValid(UsernameValidator.java:33)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:341)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:307)
at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:134)
at org.hibernate.validator.event.ValidateEventListener.onPreUpdate(ValidateEventListener.java:172)
at org.hibernate.action.EntityUpdateAction.preUpdate(EntityUpdateAction.java:217)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:65)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
at org.domain.BeReadyCounty.action.UserAccountManagerBean.findUserAccount(UserAccountManagerBean.java:104)
at org.domain.BeReadyCounty.model.UsernameValidator.isValid(UsernameValidator.java:33)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:341)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:307)
at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:134)
at org.hibernate.validator.event.ValidateEventListener.onPreUpdate(ValidateEventListener.java:172)
at org.hibernate.action.EntityUpdateAction.preUpdate(EntityUpdateAction.java:217)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:65)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
at org.domain.BeReadyCounty.action.UserAccountManagerBean.findUserAccount(UserAccountManagerBean.java:104)
at org.domain.BeReadyCounty.model.UsernameValidator.isValid(UsernameValidator.java:33)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:341)
at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:307)

Thanks for any knowledge.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 12:14 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
The session does a flush before any select, because it might be (and in this case it is actually true) that the statechange inside the session would cause the result of the select to change.


I see two options:

- Do the search in a seperate session. Nice, clean, easy
- set the flush mode on never. This is really a strange setting and might cause problems in other areas.


regards
Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 1:36 pm 
Newbie

Joined: Fri Aug 15, 2008 11:18 am
Posts: 6
Location: South Carolina
Thanks,

I started down the path of doing the find in a differnt session and it looks like the validation failure is coming from a differnt validator because the input from the password field is blank since it is a secret field. This isn't obvious from the Exception Stack, at least not to me. So I think I am going to have to reorganize what I am doing.

Thanks for getting me started on the right track.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 16, 2008 5:43 am 
Newbie

Joined: Fri Aug 15, 2008 11:18 am
Posts: 6
Location: South Carolina
Trying to do the find elsewhere had some other complications. What I ended up doing is having to set a flag just before doing the find that says skip validation. Then at the beginning of the validator the flag is checked and the validation is skipped if it is true. Then after the find the fklag is set back to false. Maybe I should have taken a whole different approach to this validation but it looked like the custom Hibernate validators was a good choice especially with the advantages of using JBoss Seam. However the Hibernate documentation led me to believe that I would not have this problem.

The Hibernate Validator Reference Guide says "objects will be checked before any inserts and before any updates are made by Hibernate. This includes changes made by cascade!". I felt the intention of this statement was to be comprehensive. So this makes it sound like calling the validators during a retrieval is a bug in Hibernate. Do other people consider this to be a Hibernate bug? It seems like calling the validators during flushing is unnecesary overhead since the validators were just called in the pre-insert and pre-update stages already.

Or do I just not understand what is going on at all.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 16, 2008 7:27 am 
Newbie

Joined: Fri Aug 15, 2008 11:18 am
Posts: 6
Location: South Carolina
After putting some println's in various places I am beginning to think this has more to do with JSF and Seam than it does with Hibernate. I remember seeing something about using phase listeners for debugging so I think I will delve into that and see if I can figure out how this Hibernate Validator stuff works with JSF and Seam. So maybe I should be posting this on the Seam user forum instead of this forum.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.