0 Replies Latest reply on Feb 28, 2008 6:01 PM by rey bumalay

    Exception thrown after overriding update on EntityHome class

    rey bumalay Newbie

      exception thrown after overriding update method of EntityHome. I'm using a custom hibernate validator that checks if a username already exist on my database.


      The entity class (only partial):



      @Length(min=3, max=32, message="Username should be 3-32 characters")
      @NotNull
      @UsernameExist
      public String getUsername() {
           return username;
      }
      
      public void setUsername(String username) {
           this.username = username;
      }




      The user home class:



      @Override
      public String update() {
           // cant seem to use hibernate validator for a very complex validation rules such as this one
           User user = getInstance();
           String oldPassword = user.getOldPassword();
           String newPassword = user.getNewPassword();
           String confirmNewPassword = user.getConfirmNewPassword();
           
           if(oldPassword.length() != 0 || newPassword.length() != 0 || confirmNewPassword.length() != 0 ) {
                if(!user.getPassword().equals(oldPassword)) {
                     getFacesMessages().add("Old password given not correct");
                     return null;
                }
                if(!newPassword.equals(confirmNewPassword)) {
                     getFacesMessages().add("New password and confirmation password did not matched");
                     return null;
                } 
                     getInstance().setPassword(getInstance().getNewPassword());
                
           }
           
           return super.update();
      }




      and finally, the username exist validator



      public boolean isValid(Object value) {
           UserHome userHome = (UserHome) Component.getInstance("userHome");
           System.out.println("RUNNING VALIDATOR...");
           User user = userHome.findUser(value.toString());
           
           // return true if no user with the given value was found
           if(user == null) {
                return true;
           } 
           
           // return true if the user retrieved is equals to the current instance in home
           if(user.getId() == userHome.getInstance().getId()) {
                return true;
           }
           
           // return false if the username exist on our database
           return true;
      }




      I noticed that the validator is called twice. I'm not sure but i think the part where i try to replace the value of the user home instance (in the user home class) causes entity be validated. see the line below:



      getInstance().setPassword(getInstance().getNewPassword());
      



      the exception btw looks like this:



      2008-02-29 00:14:53,437 FATAL [javax.enterprise.resource.webcontainer.jsf.application] java.lang.reflect.InvocationTargetException
      javax.faces.el.EvaluationException: java.lang.reflect.InvocationTargetException
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)