Hibernate.orgCommunity Documentation

Chapter 5. Grouping constraints

5.1. Requesting groups
5.2. Defining group sequences
5.3. Redefining the default group sequence
5.3.1. @GroupSequence
5.3.2. @GroupSequenceProvider
5.4. Group conversion

All validation methods on Validator and ExecutableValidator discussed in earlier chapters also take a var-arg argument groups. So far we have been ignoring this parameter, but it is time to have a closer look.

Groups allow you to restrict the set of constraints applied during validation. One use case for validation groups are UI wizards where in each step only a specified subset of constraints should get validated. The groups targeted are passed as var-arg parameters to the appropriate validate method.

Let’s have a look at an example. The class Person in Example 5.1, “Example class Person has a @NotNull constraint on name. Since no group is specified for this annotation the default group javax.validation.groups.Default is assumed.

Note

When more than one group is requested, the order in which the groups are evaluated is not deterministic. If no group is specified the default group javax.validation.groups.Default is assumed.


The class Driver in Example 5.2, “Driver” extends Person and adds the properties age and hasDrivingLicense. Drivers must be at least 18 years old (@Min(18)) and have a driving license (@AssertTrue). Both constraints defined on these properties belong to the group DriverChecks which is just a simple tagging interface.

Tip

Using interfaces makes the usage of groups type-safe and allows for easy refactoring. It also means that groups can inherit from each other via class inheritance.


Finally the class Car (Example 5.3, “Car”) has some constraints which are part of the default group as well as @AssertTrue in the group CarChecks on the property passedVehicleInspection which indicates whether a car passed the road worthy tests.


Overall three different groups are used in the example:

  • The constraints on Person.name, Car.manufacturer, Car.licensePlate and Car.seatCount all belong to the Default group
  • The constraints on Driver.age and Driver.hasDrivingLicense belong to DriverChecks
  • The constraint on Car.passedVehicleInspection belongs to the group CarChecks

Example 5.4, “Using validation groups” shows how passing different group combinations to the Validator#validate() method results in different validation results.


The first validate() call in Example 5.4, “Using validation groups” is done using no explicit group. There are no validation errors, even though the property passedVehicleInspection is per default false. However, the constraint defined on this property does not belong to the default group.

The next validation using the CarChecks group fails until the car passes the vehicle inspection. Adding a driver to the car and validating against DriverChecks again yields one constraint violation due to the fact that the driver has not yet passed the driving test. Only after setting passedDrivingTest to true the validation against DriverChecks passes.

The last validate() call finally shows that all constraints are passing by validating against all defined groups.

By default, constraints are evaluated in no particular order, regardless of which groups they belong to. In some situations, however, it is useful to control the order constraints are evaluated.

In the example from Example 5.4, “Using validation groups” it could for instance be required that first all default car constraints are passing before checking the road worthiness of the car. Finally, before driving away, the actual driver constraints should be checked.

In order to implement such a validation order you just need to define an interface and annotate it with @GroupSequence, defining the order in which the groups have to be validated (see Example 5.5, “Defining a group sequence”). If at least one constraint fails in a sequenced group none of the constraints of the following groups in the sequence get validated.


Warning

Groups defining a sequence and groups composing a sequence must not be involved in a cyclic dependency either directly or indirectly, either through cascaded sequence definition or group inheritance. If a group containing such a circularity is evaluated, a GroupDefinitionException is raised.

You then can use the new sequence as shown in in Example 5.6, “Using a group sequence”.


Besides defining group sequences, the @GroupSequence annotation also allows to redefine the default group for a given class. To do so, just add the @GroupSequence annotation to the class and specify the sequence of groups which substitute Default for this class within the annotation.

Example 5.7, “Class RentalCar with redefined default group” introduces a new class RentalCar with a redefined default group.


With this definition you can evaluate the constraints belonging to RentalChecks, CarChecks and RentalCar by just requesting the Default group as seen in Example 5.8, “Validating an object with redefined default group”.


Note

Since there must no cyclic dependency in the group and group sequence definitions one cannot just add Default to the sequence redefining Default for a class. Instead the class itself has to be added!

The Default group sequence overriding is local to the class it is defined on and is not propagated to associated objects. For the example this means that adding DriverChecks to the default group sequence of RentalCar would not have any effects. Only the group Default will be propagated to the driver association.

Note that you can control the propagated group(s) by declaring a group conversion rule (see Section 5.4, “Group conversion”).

In addition to statically redefining default group sequences via @GroupSequence, Hibernate Validator also provides an SPI for the dynamic redefinition of default group sequences depending on the object state.

For that purpose you need to implement the interface DefaultGroupSequenceProvider and register this implementation with the target class via the @GroupSequenceProvider annotation. In the rental car scenario you could for instance dynamically add the CarChecks as seen in Example 5.9, “Implementing and using a default group sequence provider”.


What if you wanted to validate the car related checks together with the driver checks? Of course you could pass the required groups to the validate call explicitly, but what if you wanted to make these validations occur as part of the Default group validation? Here @ConvertGroup comes into play which allows you during cascaded validation to use a different group than the originally requested one.

Let’s have a look at Example 5.10, “@ConvertGroup usage”. Here @GroupSequence({ CarChecks.class, Car.class }) is used to combine the car related constraints under the Default group (see Section 5.3, “Redefining the default group sequence”). There is also a @ConvertGroup(from = Default.class, to = DriverChecks.class) which ensures the Default group gets converted to the DriverChecks group during cascaded validation of the driver association.


As a result the validation in Example 5.11, “Test case for @ConvertGroup succeeds, even though the constraint on hasDrivingLicense belongs to the DriverChecks group and only the Default group is requested in the validate() call.


You can define group conversions wherever @Valid can be used, namely associations as well as method and constructor parameters and return values. Multiple conversions can be specified using @ConvertGroup.List.

However, the following restrictions apply:

  • @ConvertGroup must only be used in combination with @Valid. If used without, a ConstraintDeclarationException is thrown.
  • It is not legal to have multiple conversion rules on the same element with the same from value. In this case, a ConstraintDeclarationException is raised.
  • The from attribute must not refer to a group sequence. A ConstraintDeclarationException is raised in this situation.

Note

Rules are not executed recursively. The first matching conversion rule is used and subsequent rules are ignored. For example if a set of @ConvertGroup declarations chains group A to B and B to C, the group A will be converted to B and not to C.