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; ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.