Bean Validation support
Another feature introduced in JPA 2.0 is support for JSR 303 Bean Validation. Bean Validation support allows us to annotate our JPA entities with Bean Validation annotations. These annotations allow us to easily validate user input and perform data sanitation.
Taking advantage of Bean Validation is very simple; all we need to do is annotate our JPA Entity fields or getter methods with any of the validation annotations defined in the javax.validation.constraints package. Once our fields are annotated as appropriate, the EntityManager will prevent non-validating data from being persisted.
The following code example is a modified version of the Customer JPA entity we saw earlier in this chapter. It has been modified to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access