julien carax
julien carax

Reputation: 335

Bean Validation not working for multiple bean classes

I have a bean called Car.java which has the following fields :

private String name;
private String company;
private Maruti maruti;


@NotNull
@NotEmpty
@Size(min=5)
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

@NotNull
@NotEmpty
@Size(min=6)
public Maruti getMaruti() {
    return maruti;
}
public void setMaruti(Maruti maruti) {
    this.maruti = maruti;
}
public String getCompany() {
    return company;
}
public void setCompany(String company) {
    this.company = company;
}

where Maruti.java is another bean which has following field :

private String car_name;
private String car_model;


@NotNull
@NotEmpty
@Size(min=6)
public String getCar_name() {
    return car_name;
}
public void setCar_name(String car_name) {
    this.car_name = car_name;
}

I have added some annotations for validations in both the beans and in my main class I am calling

Set<ConstraintViolation<Car>> violations = validator
                                .validate(exchange.getIn().getBody(Car.class));

to validate the fields. Now the fields in Car.java are getting validating perfectly, but the annotations in Maruti.java doesn't work. Can anyone tell me why ?

Upvotes: 0

Views: 62

Answers (1)

artbristol
artbristol

Reputation: 32397

You need to annotate the maruti field or property with @Valid

Upvotes: 1

Related Questions