-->
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.  [ 2 posts ] 
Author Message
 Post subject: [solved] validator ignores constraints from superclasses
PostPosted: Thu Nov 25, 2010 9:19 pm 
Newbie

Joined: Thu Nov 25, 2010 7:31 pm
Posts: 4
Hello,

I've got a big problem concerning hibernate validator which keeps me up for quite a while. I haven't found any solution (or even a person having the same problem) yet.

It's the following Problem: I have an abstract class "User" and a class "Accountant" which is a subclass of User. I've annotated the properties of User with constraints (JSR-303-Annotations). When I try to validate an object of Accountant it seems that the validator ignores the constraints in the superclass.
What seems to be the oddest is that even constraint-annotations in the subclass (which I added for testing) are ignored.


User.java (excerpt):
Code:
public abstract class User {

   protected long uid;

   @NotNull
   @Size(min = 2, max = 50)
   protected String lastname;

   @NotNull
   @Size(min = 2, max = 50)
   protected String firstname;

   @NotNull
   @Email
   protected String email;

//rest ommited (incl. getter & setter)
}


The derived subclass Accountant
Code:
public class Accountant extends User {

   private boolean allowedToCreateContext;

   public boolean isAllowedToCreateContext() {
      return allowedToCreateContext;
   }

   public void setAllowedToCreateContext(boolean allowedToCreateContext) {
      this.allowedToCreateContext = allowedToCreateContext;
   }

}


Validation is done this way:
Code:
protected void validate(Object v) {
      String className = v.getClass().getSimpleName();

      Set<? extends ConstraintViolation<?>> violations = beanValidator.validate(v);
      if (violations.size() != 0) {

         StringBuilder sb = new StringBuilder();
         sb.append("[");
         for (ConstraintViolation<?> cv : violations) {
            sb.append(cv.getPropertyPath() + " ");
         }
         sb.append("]");

         log.warn("Validation of " + className + "failed (" + violations.size() + " constraint violations ->" + sb.toString() + ")");
         throw new ValidationFailedException(violations);
      }
      log.debug("Successfully validated an instance of " + className);
   }


beanValidator is injected by Spring an defined as:
Code:
<bean id="beanValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />


This code works when validating classes which are not subclasses.

I user the validator in version 4.0.2.GA.

I am really looking forward to some help from you. I just don't know where to search anymore. (Probably it's just a simple silly mistake I can't find)

Greetings,
deepthought-64


Last edited by deepthought-64 on Fri Nov 26, 2010 9:04 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: validator ignores constraints from superclasses
PostPosted: Fri Nov 26, 2010 9:04 am 
Newbie

Joined: Thu Nov 25, 2010 7:31 pm
Posts: 4
Okay.... I got it. My fault. I somehow used the wrong @NotNull annotation, which Eclipse hid from me very nicely... :)

As I said, just a simple thing... but it took me quite a while...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.