@Column(unique = true)使此参数易于验证,如@Size

时间:2017-08-02 06:53:46

标签: hibernate validation

有没有办法让@Column(unique=true)易于验证,因为它适用于@Size?如果用户名不是唯一的,我希望收到一条消息并将其显示给用户。

@Size(min = 3, max = 10, message = "username length should be between {min} and {max}")
@Column(name = "USERNAME", unique = true, nullable = false, length = 45)
private String username;

我以这种方式验证数据:

Set<ConstraintViolation<User>> constraintViolations = validator.validate(user);
if (constraintViolations.isEmpty()) {
    model.addUser(user);
} else {
    String message = "";
    for (ConstraintViolation constraintViolation : constraintViolations) {
        message += constraintViolation.getMessage() + "; ";
    }
    messageWindowController.display("Incorrect data", message, 14);
}

1 个答案:

答案 0 :(得分:0)

不,您需要编写自定义验证器和自己的约束注释。