views:

19

answers:

1

How can I get all constraints for a class. For instance I have

class A {
   @NotNull
   private SomeBean field;
}

When I call:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator(); 
Set<ConstraintDescriptor<?>> descriptor =  validator.getConstraintsForClass(formClass).getConstraintDescriptors();

The set is empty. I would assume there is already some functionality in Hibernate Validator that would give me all the information about constraints in a neat way , without me having to resort to the reflection api.

+1  A: 

getConstraintsForClass() returns constraints on the class itself. To get constraints on properties, you should call getConstrainedProperties().

axtavt
Thanks, Just a side note the getConstrainedProperties() is available in Hibernate Validator v 4.1.0Final for some reason it's not in the main repository, but you can find it here URL: https://repository.jboss.org/nexus/content/repositories/public/For those who use some king of dependency management tool like Maven anyway
pmanolov