1.2. Built in constraints

Hibernate Validator comes with some built-in constraints, which covers most basic data checks. As we will see later, you are not limited to them, you can literally in a minute write your own constraints.

Table 1.1. Built-in constraints

Annotation Apply on Runtime checking Hibernate Metadata impact
@Length(min=, max=) property (String) check if the string length match the range Column length will be set to max
@Max(value=) property (numeric or string representation of a numeric) check if the value is less than or equals to max Add a check constraint on the column
@Min(value=) property (numeric or string representation of a numeric) check if the value is more than or equals to min Add a check constraint on the column
@NotNull property check if the value is not null Column(s) are not null
@NotEmpty property check if the string is not null nor empty. Check if the connection is not null nor empty Column(s) are not null (for String)
@Past property (date or calendar) check if the date is in the past Add a check constraint on the column
@Future property (date or calendar) check if the date is in the future none
@Pattern(regex="regexp", flag=) or @Patterns( {@Pattern(...)} ) property (string) check if the property match the regular expression given a match flag (see java.util.regex.Pattern ) none
@Range(min=, max=) property (numeric or string representation of a numeric) check if the value is between min and max (included) Add a check constraint on the column
@Size(min=, max=) property (array, collection, map) check if the element size is between min and max (included) none
@AssertFalse property check that the method evaluates to false (useful for constraints expressed in code rather than annotations) none
@AssertTrue property check that the method evaluates to true (useful for constraints expressed in code rather than annotations) none
@Valid property (object) perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively. none
@Email property (String) check whether the string is conform to the email address specification none
@CreditCardNumber property (String) check whether the string is a well formatted credit card number (derivative of the Luhn algorithm) none
@Digits(integerDigits=1) property (numeric or string representation of a numeric) check whether the property is a number having up to integerDigits integer digits and fractionalDigits fractional digits define column precision and scale
@EAN property (string) check whether the string is a properly formatted EAN or UPC-A code none