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 such situations, there is no lower or upper bound depending on which annotation is not used.

How it works...

These annotations are used to control the assignment of integer 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
13.58.51.36