I want to validate the myClass
variable. The trouble is that myClass
can be either null or a reference to a myClass object. If it is a reference I want it to validate the object, if it is null obviously no validation should occur.
The following works but myClass cannot be null.
public class ParentClass {
@Valid
@NotNull
private MyClass myClass;
}
public MyClass {
@Pattern(regexp="\d.{10}")
private String myField;
}
If I remove the @NotNull
annotation from ParentClass
it no longer performs validation even if I keep the @Valid
annotation.
public class ParentClass {
@Valid
private MyClass myClass;
}
public MyClass {
@Pattern(regexp="\d.{10}") //this validation never takes place
private String myField;
}
Is is possible to validate an optionally null field using jpa/hibernate annotations?
See Question&Answers more detail:os