Java Reference
In-Depth Information
Grouping Constraints
Constraints may be added to one or more groups. Constraint groups are used to create sub-
sets of constraints so only certain constraints will be validated for a particular object. By
default, all constraints are included in the Default constraint group.
Constraint groups are represented by interfaces.
public interface Employee {}
public interface Contractor {}
Constraint groups can inherit from other groups.
public interface Manager extends Employee {}
When a constraint is added to an element, the constraint declares the groups to which
that constraint belongs by specifying the class name of the group interface name in the
groups element of the constraint.
@NotNull(groups=Employee.class)
Phone workPhone;
Multiple groups can be declared by surrounding the groups with angle brackets ( { and } )
and separating the groups' class names with commas.
Click here to view code image
@NotNull(groups={ Employee.class, Contractor.class })
Phone workPhone;
If a group inherits from another group, validating that group results in validating all con-
straints declared as part of the supergroup. For example, validating the Manager group
results in the workPhone field being validated, because Employee is a superinterface
of Manager .
Customizing Group Validation Order
By default, constraint groups are validated in no particular order. There are cases where
some groups should be validated before others. For example, in a particular class, basic
data should be validated before more advanced data.
To set the validation order for a group, add a javax.validation.GroupSequence
annotation to the interface definition, listing the order in which the validation should oc-
cur.
Search WWH ::




Custom Search