質問

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?

役に立ちましたか?

解決

NotEmpty is just a combination of @NotNull and @Size(min=1).

他のヒント

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.

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
 */

See: https://github.com/hibernate/hibernate-validator/blob/6.0/engine/src/main/java/org/hibernate/validator/constraints/NotEmpty.java

This new annotation comes from Bean Validation 2.0 (JSR 380). See:

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;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top