Validating null fields

Object references are used extensively in Java. Assigning a null value is not an uncommon practice though they need to be assigned a value before a method can be executed against it. Fields of a database may or may not be allowed to contain a null value. The @Null and @NotNull annotations can be used to indicate whether a reference field can be assigned a null value or not.

Getting ready

We will use the LicenseBean and LicenseBeanFacade classes from the ValidationApplication as discussed in the Validating persistent fields and properties recipe.

How to do it...

If we want to prevent a column of a database from being assigned a value of null, then we can enforce this with the @NotNull annotation.

@NotNull
private String name;

If the field must contain a null, then the @Null annotation is used.

@Null
private String name;

How it works...

These annotations are used to control the assignment of null values to fields. The Validator class is used in conjunction with these annotations to detect and handle violations. This technique is illustrated in the Using the Validator class recipe

See also

The Using the Validator class recipe illustrates the use of this annotation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.27