0 Replies Latest reply on Feb 15, 2016 4:03 AM by Hans-Michael Rupp

    Beans Validation Custom ConstraintValidator not being called

    Hans-Michael Rupp Novice


      I am experimenting with Beans Validation

      and my custom ConstraintValidator is never being called during JPA persisting.

      I am using Wildfly 8.2


      @Constraint(validatedBy = AdressValidator.class)

      @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })


      public @interface ValidAddress {

          String message() default "muss komplett gefüllt oder leer sein";


          Class<?>[]groups() default {};


          Class<? extends Payload>[]payload() default {};



      public class AdressValidator implements ConstraintValidator<ValidAddress, Adress> {



          public void initialize(ValidAddress arg0) {





          public boolean isValid(Adress adresse, ConstraintValidatorContext validationContext) {

              System.out.println(" Validation -------------- Adress " + adresse.toString());

              if (adresse == null)

                  return true;

              if (adresse.getTown() == null && adresse.getZip() == null && adresse.getStreet() == null)

                  return true;

              if (adresse.getTown() != null && adresse.getZip() != null && adresse.getStreet() != null)

                  return true;

              return false;







      public class Adress {



          private String street;

          private String town;

          private String zip;




      @NamedQueries({ @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e") })

      public class Employee extends GeneratedLongIdEntity {


          public final static String FIND_ALL = "Employee.findAll";




          @Size(min=2, max=50)

          private String firstName;


          @Size(min=2, max=50)

          private String lastName;



          private Adress adress;



          public void transientValidation() {

         Adress adress = new Adress(null, "Frankfurt", null);
         Employee employee = new Employee();
         try {
         } catch (Exception e) {
         assertEquals(0, employeDAO.findAll().size());



      This test fails, the employee is persisted in the DB, and isValid is never being called.