Backend Development 8 min read

Understanding @Valid vs @Validated, Group Validation, Group Sequence, and Nested Validation in Spring/Hibernate

This article explains the differences between @Valid and @Validated annotations, demonstrates how to use validation groups, group sequences, and nested validation in Spring and Hibernate Validator, and provides complete code examples illustrating each concept for robust backend data validation.

Top Architect
Top Architect
Top Architect
Understanding @Valid vs @Validated, Group Validation, Group Sequence, and Nested Validation in Spring/Hibernate

Overview

@Valid is used when applying Hibernate Validator, while @Validated leverages Spring's validation mechanism.

Java's JSR‑303 defines @Valid interfaces, and Hibernate‑Validator implements them.

@Validation wraps @Valid without functional differences, but offers extra features such as grouping, annotation placement, and nested validation.

Annotation Placement

@Validated can be placed on types, methods, and method parameters, but not on fields.

@Valid can be placed on methods, constructors, method parameters, and fields.

Group Validation

@Validated provides group functionality, allowing different validation rules for different groups.

@Valid does not support groups.

Example of defining groups:

public interface IGroupA {}
public interface IGroupB {}

Bean with group‑specific constraints:

public class StudentBean implements Serializable {
    @NotBlank(message = "用户名不能为空")
    private String name;
    @Min(value = 18, message = "年龄不能小于18岁", groups = {IGroupB.class})
    private Integer age;
    @Pattern(regexp = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$", message = "手机号格式错误")
    private String phoneNum;
    @Email(message = "邮箱格式错误")
    private String email;
    @MyConstraint
    private String className;
}

Controller testing group IGroupA :

@RestController
public class CheckController {
    @PostMapping("stu")
    public String addStu(@Validated({IGroupA.class}) @RequestBody StudentBean studentBean) {
        return "add student success";
    }
}

When multiple groups are specified, validation proceeds according to the defined groups.

Group Sequence

Define a sequence to enforce ordered validation; if a preceding group fails, subsequent groups are skipped.

@GroupSequence({Default.class, IGroupA.class, IGroupB.class})
public interface IGroup {}

Bean with constraints assigned to groups IGroupA (age) and IGroupB (className) will be validated in the defined order.

Nested Validation

To validate a nested object, annotate the containing field with @Valid . @Validated cannot be used for nested validation.

Example of nested beans:

public class TeacherBean {
    @NotEmpty(message = "老师姓名不能为空")
    private String teacherName;
    @Min(value = 1, message = "学科类型从1开始计算")
    private int type;
}

public class StudentBean implements Serializable {
    @NotBlank(message = "用户名不能为空")
    private String name;
    @Min(value = 18, message = "年龄不能小于18岁")
    private Integer age;
    @Pattern(regexp = "...", message = "手机号格式错误")
    private String phoneNum;
    @Email(message = "邮箱格式错误")
    private String email;
    @MyConstraint
    private String className;
    @Valid
    @NotNull(message = "任课老师不能为空")
    @Size(min = 1, message = "至少有一个老师")
    private List
teacherBeans;
}

Without @Valid on teacherBeans , only @NotNull and @Size are applied; adding @Valid triggers validation of each TeacherBean instance.

Conclusion

The article demonstrates how to choose between @Valid and @Validated , apply validation groups, define group sequences for ordered checks, and perform nested validation in Spring/Hibernate applications, enabling precise and flexible backend data validation.

JavaBackend DevelopmentSpringAnnotationsGroup ValidationHibernate Validation
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.