Javax validation on nested objects - not working
Use @ConvertGroup
from Bean Validation 1.1 (JSR-349).
Introduce a new validation group say Pk.class
. Add it to groups
of BuildingDto
:
public class BuildingDto {
@NotNull(groups = {Pk.class, Existing.class, LocationGroup.class})
// Other constraints
private Integer id;
//
}
And then in LocationDto
cascade like following:
@Valid
@ConvertGroup.List( {
@ConvertGroup(from=New.class, to=Pk.class),
@ConvertGroup(from=LocationGroup.class, to=Pk.class)
} )
// Other constraints
private BuildingDto building;
Further Reading:
5.5. Group conversion from Hibernate Validator reference.
Just try adding @valid
to collection. it would be working as per reference hibernate
@Getter
@Setter
@Valid
@NotNull(groups = { Existing.class })
private List<LocationDto> locations;
@Valid annotation must be added to cascade class attributes.
LocationDTO.class
public class LocationDto {
@Valid
private BuildingDto building;
.........
}