Java Reference
In-Depth Information
Constraint
Supported type
Description
java.util.Date and
java.util.Calendar
This ensures that the date
contained in the object is later
than the date of treatment
@Future
This ensures that value of the
item meets the regular expres-
sion defined in the annotation
@Pattern
CharSequence
One advantage of this specification is the ability to define the different constraints it
offers via annotations, which facilitates its use. According to the characteristics of the
annotation (explained in detail in Chapter 7 , Annotations and CDI ), you can express
constraints for a class, field, or property. The following example shows a Student
entity whose fields are decorated with the built-in constraints. You can see the con-
straints to avoid the null value or to define the size and format of attributes in the
following code:
@Entity
public class Student implements Serializable {
@Id
@NotNull
@Size(min = 1, max = 15)
private String id;
@Size(max = 30)
private String firstname;
@Pattern(regexp="^\\(?(\\d{3})\\)?[-
]?(\\d{3})[- ]?(\\d{4})$", message="Invalid
phone/fax format,should be as xxx-xxx-xxxx")
//if the field contains phone or fax number
consider using this//annotation to enforce
field validation
@Size(max = 10)
Search WWH ::




Custom Search