I have been using Hibernate for quite a number of projects now mostly all of them with JSF for implementing the web interface. I’d always found it boring to do the “form validation thing” - mostly it is look at the constraints in the mapping definition and attach the proper validation tag to a form input element.

So today I had a look at the Hibernate Validator project that promises to make those validation constraints  from the model available across all layers of application code. The project provides plenty of annotation types for annotating the domain classes. In general I try not to overuse annotations, especially those for mapping domain attributes to database entities - but this kind of annotations make sense to me!

As you would expect Hibernate Validations integrates with Hibernate core of course and JPA implementations so all of your annotated elements will be validated when persisted.

To perform application level validation a validation API is provided for validating bean instances or single bean properties.

ClassValidator validator = new ClassValidator(Car.class);

InvalidValue[] validationMessages = validator .getInvalidValues(porsche);
InvalidValue[] propertyMessages = validator .getInvalidValues(porsche, "engine");

Overall the API looks fairly slim and simple! In the next days I will investigate if the API allows “nested properties”. I’m thinking of creating some JSF tag (like the Seam tags) to perform validation of complete forms :)