HibernateValidator constraintMapping for the fields inside nested object structure

320 Views Asked by At

I have below java external class which is coming from third party jar and I want to do the bean validation against this class.

Product.java:

private String id;
private Price price

Price.java

private String price1;
private String price2;

Below is the hibernate validator for the same:

private Validator createDataValidator() {
    HibernateValidatorConfiguration hibernateValidatorConfiguration = Validation
        .byProvider(HibernateValidator.class)
        .configure();
    ConstraintMapping constraintMapping =
            hibernateValidatorConfiguration.createConstraintMapping();
    constraintMapping
        .type(Product.class)
        .field("id")
        .constraint(new NotNullDef().message("id should not be null"))

        .type(Price.class)
        .field("price1")
        .constraint(new NotNullDef().message("price1 should not be null"))

        .field("price2")
        .constraint(new NotNullDef().message("price2 should not be null"))

    Validator validator =
            hibernateValidatorConfiguration.addMapping(constraintMapping)
                .buildValidatorFactory()
                .getValidator();
    return validator;
}

For nested class of Price the validation is not happening as expected, help is much appreciated.

0

There are 0 best solutions below