0

BeanValidation works on normal Validations which are placed at FIELD-level:

@Email
@Column(unique = true)
private String email;

However, placing a custom constraint on TYPE-Level:

@Entity
@UniqueEmail
public class UserAccount { 

  @Email
  @Column(unique = true)
  private String email;
... 
}

with the annotation being:

@Constraint(validatedBy = {UniqueEmailValidator.class})
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface UniqueEmail { ... }

will not work. The UniqueEmailValidator is never called unless I programatically trigger it by calling .validate() on an injected javax.validation.Validator

Is this expected behaviour or can that be changed in a way that the BeanValidation will also evaluate custom constraints on type level.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
chzbrgla
  • 5,158
  • 7
  • 39
  • 56
  • 1
    If you are talking about Bean Valiadtion in general, type level constraints are for sure executed, especially for calls to Validator#validate calls. Any chance you are using Bean Validation with JSF? In this case this question is really a duplicate of for example http://stackoverflow.com/questions/11972419/cross-field-bean-validation-why-you-no-work. JSF does not call Validator#validate, but rather Validator#validateProperty. – Hardy Feb 25 '14 at 21:04
  • Yes, I'm using the BeanValidation with JSF - had that in the titel originally. Thanks for the redirect. – chzbrgla Feb 26 '14 at 08:41

0 Answers0