Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply a @NotNull constraint to all fields in a class using javax.validation?

I'm using the hibernate implementation of the javax.validation and would like to know how whether anyone has been able to apply the @NotNull constraint annotation to all fields of a class, rather than to each individual field?

I would prefer to enforce the @NotNull constraint as a default across my project, to avoid littering the code with @NotNull annotations on every field. Ideally it would be preferable to define a custom constraint "@MayBeNull" to annotated any fields that may be null.

like image 966
Paco Avatar asked Oct 14 '22 00:10

Paco


1 Answers

Does it really make sense in your case to validate all fields against null? What about for example with basic types like int, long, etc

Anyways, Bean Validation does not offer the functionality you are asking for. You could write a custom class constraint @MyNotNull which via reflection validated all fields against null. Personally I recommend against it.

like image 84
Hardy Avatar answered Oct 18 '22 10:10

Hardy