2017-08-02 58 views
0

有沒有什麼辦法使@Column(unique=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); 
} 

回答

0

不,你需要編寫自定義驗證和你自己的約束註解。