javagenericsbean-validation

How do I construct a ConstraintViolationException in Bean Validation 1.0?


I am puzzled by the javax.validation API. I am writing a simple test to understand it:

Sample sample = new Sample();
Set<ConstraintViolation<Sample>> violations = validator.validate(sample);
if (!violations.isEmpty()) {
    // Eclipse refuses to let me use my violations variable
    throw new ConstraintViolationException(violations);
}

How should I declare the set of violations so I can use it in my exception constructor?


Solution

  • You can work around this like so:

    throw new ConstraintViolationException(
        new HashSet<ConstraintViolation<?>>(violations));
    

    You may be interested in tracking BVAL-198 which addresses this issue.