There are three Java classes defined for the examples of bean validation demonstrated in this post. One class, Car.java is adapted from the example provided on the "Getting started with Hibernate Validator" page and its code listing is shown next.

Another class used in this post's examples is defined in Garage.java and is mostly a wrapper of multiple instances of Car. Its primary purpose is to help illustrate recursive validation supported by Hibernate Bean Validator.

The Garage code listing above uses the @Valid annotation to indicate that the Car instances held by the class should also be validated ("validation cascading").

The final Java class used in this post's examples is the class that will actually perform the validation of the two bean validation annotated classes Car and Garage. This class's listing is shown next.

The above code features several calls to javax.validation.Validator.validate(T, Class<?>) that demonstrate the effectiveness of the annotations on the classes whose instances are being validated. Several examples validate an object's single validation violation, an example validates an object's multiple validation violations, and a final example demonstrates successful cascading violation detection.

The class HibernateValidatorDemonstration has a main(String[]) function that can be executed in a Java SE environment (assuming the necessary JARs are on the runtime classpath). The output of running the above demonstration class is shown next:

Jul 19, 2015 9:30:05 PM org.hibernate.validator.internal.util.Version
INFO: HV000001: Hibernate Validator 5.1.3.Final
Null Manufacturer Example
manufacturer may not be null
Null License Example
licensePlate may not be null
Too Few Seats Example
seatCount must be greater than or equal to 2
Too Few License Digits Example
licensePlate size must be between 2 and 14
Null Manufacturer and Null License Example
manufacturer may not be null
licensePlate may not be null
No Cars in Garage
cars size must be between 1 and 2147483647
Messed Up Cars in Garage
cars[].licensePlate size must be between 2 and 14
cars[].manufacturer may not be null
cars[].licensePlate may not be null
cars[].seatCount must be greater than or equal to 2

Conclusion

This post has demonstrated that the Hibernate Bean Validator, the reference implementation of the Bean Validation specification, can be executed in a Java SE environment. As part of this demonstration, some basic concepts associated with the Bean Validation specification and the Hibernate Bean Validator implementation were also discussed and demonstrated.