Chapter 4. Additional modules

Hibernate Annotations mainly focus on persistence metadata. The project also have a nice integration with two Hibernate modules.

4.1. Hibernate Validator

4.1.1. Description

Annotations are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the bean itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, Hibernate Validator has been designed for that purpose.
Hibernate Validator works at two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the Hibernate metamodel and incorporate them into the generated database schema.
Each constraint annotation is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the Hibernate metamodel, allowing Hibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by Hibernate. Hibernate Validator is not limited to use with Hibernate. You can easily use it anywhere in your application.
When checking instances at runtime, Hibernate Validator returns information about constraint violations in an array of InvalidValue s. Among other information, the InvalidValue contains an error description message that can embed the parameter values bundle with the annotation (length limit, for example), and message strings that may be externalized to a ResourceBundle .

4.1.2. Integration with Hibernate Annotations

If Hibernate Validator (hibernate-validator.jar) is available in the classpath, Hibernate Annotations will integrate in two ways:
  • Constraints will be applied to the Data Definition Language. In other words, the database schema will reflect the constraints (provided that you use the hbm2ddl tool).
  • Before an entity change is applied to the database (insert or update), the entity is validated. Validation errors, if any, will be carried over through an InvalidStateException.
For entities free of validation rules, the runtime performance cost is null.
To disable constraint propagation to DDL, set up hibernate.validator.apply_to_ddl to false in the configuration file. Such a need is very uncommon and not recommended.
To disable pre-entity change validation, set up hibernate.validator.autoregister_listeners to false in the configuration file. Such a need is very uncommon and not recommended.
Check the Hibernate Validator reference documentation for more information.