12.6.3.4. Hibernate Validator Constraints
Table 12.11. Built-in Constraints
Annotation | Apply on | Runtime checking | Hibernate Metadata impact |
---|---|---|---|
@Length(min=, max=) | property (String) | Check if the string length matches 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 equal 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 equal 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 matches 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. |
property (String) | Check whether the string is conform to the e-mail 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. |