| Given the contraint mapping constraints-config.xml:
<constraint-mappings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping/validation-mapping-1.1.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">
<bean class="mypackage.MyClass">
<getter name="id">
<constraint annotation="org.hibernate.validator.constraints.NotBlank"/>
</getter>
</bean>
</constraint-mappings>
Where MyClass extends from MyObject
public class MyObject {
private final String id;
public MyObject(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
public class MyClass extends MyObject {
public MyClass(String id) {
super(id);
}
}
The Validator built using the constraint mappings xml, ignores the getter Constraint
class Main {
static void main(String[] args) {
MyClass myinstance = new MyClass("");
Set<ConstraintViolation<MyClass>> constraintViolations = getValidator().validate(myinstance);
}
static final Validator getValidator() {
final Configuration<?> config = Validation.byDefaultProvider().configure();
try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("contraints-config.xml")) {
config.addMapping(inputStream);
return config.buildValidatorFactory().getValidator();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
}
No constraint validations are reported even if the id is blank But if I add to the constraints-config.xml, a group-sequence element with two entries with the same group value using the class name, then it works. And I think the reason is in the org.hibernate.validator.internal.engine.ValidatorImpl So if I add the group-s
<constraint-mappings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping/validation-mapping-1.1.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">
<bean class="mypackage.MyClass">
<group-sequence>
<value>mypackage.MyClass</value>
<value>mypackage.MyClass</value>
</group-sequence>
<getter name="id">
<constraint annotation="org.hibernate.validator.constraints.NotBlank"/>
</getter>
</bean>
</constraint-mappings>
Now the validation works, and reports a ConstraintValidation |