Recalling the first approach, and rather than programmatically creating the validator object by creating a ValidatorFactory using the Validation class and so on, you can simply inject the Validator object as a dependency using the CDI, as shown in the following example:
@RequestScoped
public class MovieBean {
@PersistenceContext(name = "jpa-examplesPU")
private EntityManager entityManager;
@Inject
private Validator validator;
public void validateMovie() {
Movie movie = new Movie();
Set<ConstraintViolation<Movie>> violations = validator.validate(movie);
for (ConstraintViolation<Movie> violation : violations) {
System.out.println(violation.getPropertyPath());
System.out.println(violation.getMessage());
}
}
}
As you see...