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
AccountantCode:
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