java - Programmatic and declarative Constraint declaration -
i'm validating beans through kind of code:
validator validator = validation.builddefaultvalidatorfactory().getvalidator(); validator.validatevalue(class, propertyname, value);
my classes this:
public static interface primitive { public primitive setstring(string s); @notnull public string getstring(); }
this working fine far. seems it's not possible plain hibernate validator constraint definitions/mapping given class , add additional constraints described here. looks constraint mapping manual, don't todo. on other side beandescriptor validator.getconstraintsforclass(class)
seems not usable constraintmapping.
this have in mind:
constraintmapping mapping = new constraintmapping(); mapping .type(order.class).getconstraints()/*reads constraints declared on bean*/ .property("customer", elementtype.field)/*add additional constraints*/ .constraint(notnulldef.class); validator validator = validation .byprovider(hibernatevalidator.class) .configure() .addmapping(mapping) .buildvalidatorfactory() .getvalidator();
the programmatic constraint api additive, meaning don't need suggested getconstraints. see validator online docs - http://docs.jboss.org/hibernate/stable/validator/reference/en-us/html_single/#section-programmatic-api
by default, constraints added via fluent api additive constraints configured via standard configuration capabilities. possible ignore annotation , xml configured constraints required.
Comments
Post a Comment