13.6. Bean Validation
13.6.1. About Bean Validation Copy linkLink copied to clipboard!
bean-validation
quickstart example: Section 1.4.1.1, “Access the Quickstarts”.
13.6.2. Hibernate Validator Copy linkLink copied to clipboard!
13.6.3. Validation Constraints Copy linkLink copied to clipboard!
13.6.3.1. About Validation Constraints Copy linkLink copied to clipboard!
13.6.3.2. Create a Constraint Annotation in Red Hat JBoss Developer Studio Copy linkLink copied to clipboard!
This task covers the process of creating a constraint annotation in Red Hat JBoss Developer Studio, for use within a Java application.
Prerequisites
Procedure 13.5. Create a Constraint Annotation
- Open a Java project in Red Hat JBoss Developer Studio.
Create a Data Set
A constraint annotation requires a data set that defines the acceptable values.- Right click on the project root folder in the Project Explorer panel.
- Select
. - Configure the following elements:
- Package:
- Name:
- Click thebutton to add any required interfaces.
- Clickto create the file.
- Add a set of values to the data set and click.
Example 13.25. Example Data Set
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the Annotation File
Create a new Java class.- Configure the constraint annotation and click.
Example 13.26. Example Constraint Annotation File
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
- Result
- A custom constraint annotation with a set of possible values has been created, ready to be used in the Java project.
13.6.3.3. Hibernate Validator Constraints Copy linkLink copied to clipboard!
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. |
13.6.4. Configuration Copy linkLink copied to clipboard!
13.6.4.1. Example Validation Configuration File Copy linkLink copied to clipboard!
Example 13.27. validation.xml