Hibernate Validator 4+. Get all Constraints associated with a class
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开发者_C百科 to resort to the reflection api.
getConstraintsForClass()
returns constraints on the class itself. To get constraints on properties, you should call getConstrainedProperties()
.
精彩评论