Validating string fields

Strings are used extensively in many Java applications. When strings are persisted to a database, the database column may be restricted in size. The entity can use annotations to enforce a size constraint of these fields.

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

We can specify the size of a String using the @Size annotation. This annotation has a min and a max attribute. The min attribute specifies the minimum length allowed while the max attribute specifies the upper bound on the length of the string. Here we specify a minimum length of 12 characters and a maximum length of ...

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.