Bean Validation support
Bean Validation is a Jakarta EE specification consisting of a number of annotations used to simplify data validation. Jakarta Persistence Bean Validation support allows us to annotate our 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 Jakarta Persistence Entity fields or getter methods with any of the validation annotations defined in the jakarta.validation.constraints
package. Once our fields are annotated as appropriate, EntityManager
will prevent non-validating data from being persisted.
The following code example is a modified version of the Customer
Jakarta Persistence entity we saw earlier in this chapter. It has been modified to take advantage of Bean Validation in some of its fields:
package com.ensode.jakartaeebook.beanvalidation.entity; //imports omitted for brevity @Entity...