How do I construct a ConstraintViolationException?

Raylite3 picture Raylite3 · Aug 23, 2012 · Viewed 11.7k times · Source

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?

Answer

Gunnar picture Gunnar · Aug 23, 2012

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.