I'm currently validating my beans through this kind of code:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
validator.validateValue(class, propertyName, value);
My classes look like this:
public static interface Primitive {
public Primitive setString(String s);
@NotNull
public String getString();
}
This is working fine so far. But it seems that it's not possible with plain hibernate validator to get the Constraint Definitions/Mapping from a given Class and to add additional constraints like described here. It looks like the constraint mapping is all manual, which I don't like todo. On the other side the BeanDescriptor which I get with Validator.getConstraintsForClass(class)
seems to be not usable with ConstraintMapping.
This is what I have in mind:
ConstraintMapping mapping = new ConstraintMapping();
mapping
.type(Order.class).getConstraints()/*reads the constraints declared on the Bean*/
.property("customer", ElementType.FIELD)/*add additional constraints*/
.constraint(NotNullDef.class);
Validator validator = Validation
.byProvider(HibernateValidator.class)
.configure()
.addMapping(mapping)
.buildValidatorFactory()
.getValidator();