Validating Integer fields

Integers are widely used in most applications. When these variables are persisted, it is useful to be able to limit their values to a specific range. The @Min and @Max annotations are used for this purpose.

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

The size of an integer field can also be validated using the @Min and @Max annotations. The monthsToExpire field in the following declaration must have a value between 12 and 48 inclusive to avoid a constraint violation.

@Min(12)
@Max(48)
private int monthsToExpire;

The @Min or @Max annotations can be used without the other one. In ...

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.