user4460845
user4460845

Reputation:

java/beans validation - collection/map does not contain nulls

There is the @NotNull annotation which validates that a certain object is not null.

There is the @NotEmpty annotation which validates that a certain collection/map/string/... is not empty.

Is there also an annotation which valides that a certain collection/map does not contain any nulls? I am unable to find it. It seems so basic, that I believe it must be in the JSR-303 spec.

Upvotes: 8

Views: 6890

Answers (3)

M. Justin
M. Justin

Reputation: 21094

Java 8 added the ability to apply annotations directly to generic type parameters (e.g. private List<@SomeAnnotation String> myList).

In a Java 8 application, Bean Validation 2.0/Hibernate Validator 6.0 allows using the standard bean validation annotations such as @NotNull on type parameters of collection types to apply that validation to each element in the collection or map. This feature can be used to achieve the desired result of ensuring no null elements are within a collection or map.

Hibernate Validator 5.2 (which implements Bean Validation 1.2) has limited support for bean validation annotations on type parameters of collection types, though the standard bean validation annotations cannot be used in this manner, nor can such annotations be used on map keys.

Bean Validation 2.0/Hibernate Validator 6.0

Bean Validation 2.0 (of which Hibernate Validator 6.0 is the reference implementation) allows using its validation annotations directly on generic type arguments. This is noted in the Hibernate 6.0 release documentation:

Hibernate Validator 6.0 is the Reference Implementation of the Bean Validation 2.0 specification so it comes with all its new features:

  • First class support of container element constraints and cascaded validation (think private Map<@Valid @NotNull OrderCategory, List<@Valid @NotNull Order>> orderByCategories;);

If the project is using Java 8 with Bean Validation 2.0, this feature can be used to achieve your stated goals:

List<@NotNull String> noNullsList;
Map<@NotNull String, @NotNull String> noNullKeysOrValuesMap;

Bean Validation 1.2/Hibernate Validator 5.2

Hibernate 5.2 (with Bean Validation 1.2) added a limited version of the feature to allow validation annotations directly on generic type arguments. However, none of its built-in Bean Validation or Hibernate Validation constraints could be used in this manner, as the annotations do not specify ElementType.TYPE_USE for backwards-compatibility reasons. Additionally, type argument constraints could be specified for map values but not map keys. This is all described in the Hibernate Validator 5.2 documentation:

Starting from Java 8, it is possible to specify constraints directly on the type argument of a parameterized type. However, this requires that ElementType.TYPE_USE is specified via @Target in the constraint definition. To maintain backwards compatibility, built-in Bean Validation as well as Hibernate Validator specific constraints do not yet specify ElementType.TYPE_USE.

[...]

When applying constraints on an Iterable type argument, Hibernate Validator will validate each element.

[...]

Type argument constraints are also validated for map values. Constraints on the key are ignored.

Summary

In summary, if you are using Java 8 with Bean Validation 2.0 (such as Hibernate Validator 6), you can annotate the generic list & map type arguments with @NotNull.

If you are using Java 8 with Bean Validation 1.2 and Hibernate Validator 5.2, you can write a custom @NotNull validation annotation with TYPE_USE in its definition, and apply it to the generic type of the collection or map value.

If you are not on Java 8 or are on a version of Hibernate Validator prior to 5.2, you would need a custom constraint to apply to the map or list to verify that every element of the collection or map is non-null.

Upvotes: 16

Hardy
Hardy

Reputation: 19109

There is no such built-in constraints. You can easily write your custom constraints, eg @NoNullElements, which does what you want. Refer to the Refer to the documentation http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-customconstraints to see how to write custom constraints.

Upvotes: 2

Andy_Lima
Andy_Lima

Reputation: 159

I had the same Problem. And the only solution I found was, adding a null validation into the setters of the Entity. If the submitted value is null -> return. I know thats pretty ugly but the only way I know how to avoid exceptions.

Upvotes: 0

Related Questions