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



有没有办法使@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);
}

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

最新更新