Search code examples
javahibernate-validator

Hibernate Validator and apache bval - which validator to load?


Project has both bval and hibernate validator 5.3.6Final.

bval comes from a 3rd party jar dependency

When trying to load Validator - its defaulting to bval and fails at validation constraint defined by hibernate annotation.

final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        ValidatorFactory cf = Validation.buildDefaultValidatorFactory();
        final Set<ConstraintViolation<AdminLoginFormImpl>> violations = validator.validate(this)
  1. How to load exclusively hibernate validator ?

  2. Can they even co-exist ?

Exception seen ::

No compliant org.hibernate.validator.constraints.Length ConstraintValidator found for annotated element of type java.lang.String

javax.validation.UnexpectedTypeException: No compliant org.hibernate.validator.constraints.Length ConstraintValidator found for annotated element of type java.lang.String

org.apache.bval.jsr.job.ComputeConstraintValidatorClass.findAnnotatedElementValidator(ComputeConstraintValidatorClass.java:171)

Solution

  • Apparently, your code uses one of the Hibernate Validator-specific constraints so using Apache BVal won't work.

    If your code doesn't use any Apache BVal-specific features, I would stick to Hibernate Validator and exclude Apache Bval from the dependencies.

    In any case, only one of the implementations will be used so you should only have one.