This article discusses how to use the Hibernate Validator for your Java classes.
Here are the steps to implement the validator:
- Add hibernate-validator.jar to the build path.
- In the POJO that you want to validate, import org.hibernate.Validator.*.
- Using annotations, set the conditions for validation on the getter method of the property to check. The “message” attribute can be set to a hard-coded string or a property from a resource bundle.
- Create a resource bundle with some entries for the validation edit messages (this is optional – skip this step if you want to use the built-in Hibernate Validator messages).
- Now it’s time to actually call the validator to validate our code. Suppose we have some business logic that validates the last name. First of all, we need to load the resource bundle that contains our validation edit messages (again, you can skip this step if you are using the built-in Hibernate Validator messages). The resource bundle is typically cached.
- Next, we have to create an instance of the Validator class and use it to validate the data. This should be done once per class, so you should probably use a static instance.
import org.hibernate.Validator.*; ... ... @NotNull(message = "{person.lastName.notNull}") @Length(min = 1, max = 8) public String getLastName() { return lastName; }
person.lastName.notNull=The last name must not be null.
//get a ResourceBundle – this should be done once and cached ResourceBundle rb = null; try { rb = ResourceBundle.getBundle("messages", Locale.ENGLISH); } catch (MissingResourceException e) { // The resource bundle cannot be found or // the key does not exist in the resource bundle System.out.println(e.getMessage()); return false; }
//Create a Validator – this should be done once per validator, so //it should probably be a singleton ClassValidator<Person> personValidator = new ClassValidator<Person>(Person.class, rb); InvalidValue[] validationMessages = personValidator.getInvalidValues(person); for (InvalidValue value : validationMessages) { System.out.println(value); //Will print the message }
Note that the call to the validator returns an array of InvalidValue objects. In our simple example, we are just looping through the array and displaying each error in the console. You might want to cast the array to another data type, or log each error to a file, or save it to a session error object, etc.
Here’s a full view of the code to get a resource bundle and pass it to the validator, then validate the bean:
ResourceBundle rb = null; try { rb = ResourceBundle.getBundle("messages", Locale.ENGLISH); } catch (MissingResourceException e) { // The resource bundle cannot be found or // the key does not exist in the resource bundle System.out.println(e.getMessage()); return false; } ClassValidator<Person> personValidator = new ClassValidator<Person>(Person.class, rb); InvalidValue[] validationMessages = personValidator.getInvalidValues(person); for (InvalidValue value : validationMessages) { System.out.println(value); //Will print the message }
Notes
For more info, check out the Hibernate Validator page. As of version 4.x Hibernate Validator is based on a new code base which is the reference implementation for JSR 303: Bean Validation. Finally, bean validation will be part of the Java language.
Happy coding!