Reputation: 14466
Is there a way to implement @NotEmpty
hibernate validation without writing custom validation?
javax.validation package does not contain this annotation. Only @NotNull
. But it does not validate for Non-null but empty values. So I would like to see an alternative for @NotEmpty
.
Using @Pattern
? How?
Upvotes: 41
Views: 46344
Reputation: 5336
In the Hibernate @NotEmpty
source code after Hibernate 6, it told us use the standard javax.validation.constraints.NotEmpty
constraint instead:
/**
* Asserts that the annotated string, collection, map or array is not {@code null} or empty.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*
* @deprecated use the standard {@link javax.validation.constraints.NotEmpty} constraint instead
*/
This new annotation comes from Bean Validation 2.0 (JSR 380). See:
Upvotes: 2
Reputation: 1271
For Hibernate it is deprecated in the newer version.
With the newer version of Javax validation it has @Empty
Use
import javax.validation.constraints.NotEmpty;
@NotEmpty
private List<Record> records;
Upvotes: 1
Reputation: 1097
Please be aware that @NotEmpty will return valid for a List<> containing a null element.
Kind of bizarre in the case of a @QueryParam List<>
As say Affe, I did a custom annotation, itself annotated with @NotNull and @Size(min=1) with a custom validator that iterates the collection and positions a boolean flag only if the elements are not null.
Upvotes: 9