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

      Hello,

      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 })

      @Retention(RetentionPolicy.RUNTIME)

      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> {

       

          @Override

          public void initialize(ValidAddress arg0) {

       

          }

         

          @Override

          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;

          }

       

      }

       

      @Embeddable

      @ValidAddress

      public class Adress {

         

       

          private String street;

          private String town;

          private String zip;

       

      ------------------------------------------------------------------------------------------------

      @Entity

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

      public class Employee extends GeneratedLongIdEntity {

       

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

       

         

          @NotNull

          @Size(min=2, max=50)

          private String firstName;

          @NotNull

          @Size(min=2, max=50)

          private String lastName;

       

          @ValidAddress

          private Adress adress;

      -------------------------------------------------------------------------------

      @Test

          public void transientValidation() {

         Adress adress = new Adress(null, "Frankfurt", null);
         Employee employee = new Employee();
         employee.setFirstName("Marcus");
         employee.setLastName("Didius");
         employee.setAdress(adress);
         try {
         employeDAO.persist(employee);
         } catch (Exception e) {
         assertTrue(ExceptionUtil.isConstraintViolationException(e));
         }
         assertEquals(0, employeDAO.findAll().size());

          }

       

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