内置的约束条件
Hibernate Validator包含了一些基本的使用比较广的约束,下面是一些Hibernate Validator给出的最常用的约束.另外Hibernate Validator还提供了一些有有的custom约束.
一.Bean Validation constraints
下面是一些常的constraints,它们的作用,支持的数据类型.它们全部都是属性级别的注释,如果你使用Hibernate 对象-关系映射,当你为你的model创建ddl时,有一些约束是要被考虑在内的.
注意:Hibernate Validator的有些约束是能支持比Bean Validation specification要求的数据类型的更多的类型.e.g. @Max can be applied to Strings
Annotation |
Supported data types |
作用 | Hibernate metadata impact |
@AssertFalse | Boolean , boolean |
Checks that the |
没有 |
@AssertTrue | Boolean , boolean |
Checks that the |
没有 |
@DecimalMax |
BigDecimal , of Number and |
被标注的值必须 |
没有 |
@DecimalMin |
BigDecimal , |
被标注的值必须 |
没有 |
@Digits(integer=, |
BigDecimal , |
Checks whether the |
对应的数据库表 |
@Future |
java.util.Date , |
检查给定的日期是否 |
没有 |
@Max |
BigDecimal , |
检查该值是否大于或 |
会给对应的数据库表 |
@Min |
BigDecimal , |
检查该值是否大于或 |
的最小值. |
@NotNull | Any type |
Checks that the |
对应的表字段不允许 |
@Null | Any type |
Checks that the |
没有 |
@Past |
java.util.Date , of ReadablePartial |
检查标注对象中的值 |
没有 |
@Pattern(regex=, |
String . Additionally |
检查该字符串是否能 |
没有 |
@Size(min=, max=) |
String , Collection , |
Checks if the |
对应的数据库表字段 |
@Valid |
Any non-primitive |
递归的对关联对象进 |
没有 |
注意: 这些约束都能支持message,groups,payload的参数 |
二,Additional constraints
除了Bean Validation API Hibernate Validator中的约束外,还有一些有用的定制的约束.其中@ScriptAssert是一个类级别的注释
Annotation |
Supported data |
作用 |
Hibernate metadata |
@CreditCardNumber | CharSequence |
Checks that the test. Note, this |
没有 |
CharSequence |
Checks whether the |
没有 | |
@Length(min=, max=) | CharSequence |
Validates that |
对应的数据库表字段 |
@ModCheck(modType=, |
CharSequence |
Checks that the multiplier |
没有 |
@NotBlank | CharSequence |
Checks that the whitespaces are |
没有 |
@NotEmpty |
CharSequence , |
Checks whether the |
没有 |
@Range(min=, max=) |
BigDecimal , |
Checks whether |
没有 |
@SafeHtml(whitelistType=, |
CharSequence |
Checks whether |
没有 |
@ScriptAssert(lang=, |
Any type |
Checks whether the element. In |
没有 |
@URL(protocol=, |
CharSequence |
Checks if the which the URL must |
没有 |
三, Country specific constraints(略)