|
Hibernate Validator
Following the DRY (Don't Repeat Yourself) principle, Hibernate Validator let's you express your domain constraints once (and only once) and ensure their compliance at various level of your system automatically. Annotations are a very convenient and elegant way to specify invariant constraints on the domain model implementation, the persistent classes. Hibernate Validator comes bundled with a set of common validations (@NotNull, @Email, @Max, and so on), and you can build you own validation rules very easily. This is an example of an annotated persistent class:
public class Address {
@NotNull private String line1;
private String line2;
private String zip;
private String state;
@Length(max = 20)
@NotNull
private String country;
@Range(min = -2, max = 50, message = "Floor out of range")
public int floor;
...
}
Hibernate Validator integrates with Hibernate by applying the constraints on the database schema (DDL generation) and by checking entity validity before Hibernate inserts or updates instances. You can use Hibernate Validator with any Java Persistence provider, not only Hibernate, although you will not be able to use automatic DDL alteration for constraint generation outside of Hibernate EntityManager. Hibernate Validator constraint checking can be triggered programmatically, for example by the business layer. JBoss Seam makes use of this capability and integrates it with JSF, providing end to end validation from the presentation level down to the database constraints with a unique definition and declaration of integrity rules. Hibernate Validator is fully internationalized. The project lead of Hibernate Validator is an expert group member of JSR 303: Bean Validation. |
||||||||||||||||