內置的約束條件
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 annotated element is false . |
沒有 |
@AssertTrue |
Boolean , boolean |
Checks that the annotated element is true . |
沒有 |
@DecimalMax |
BigDecimal , BigInteger , String , byte , short , int , long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number and CharSequence . |
被標注的值必須 不大於約束中指定 的最大值. 這個約 束的參數是一個通 過 BigDecimal 定義的最 大值的字符串表示. |
沒有 |
@DecimalMin |
BigDecimal , BigInteger , String , byte , short , int , long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number and CharSequence . |
被標注的值必須 不小於約束中指定 的最小值. 這個約 束的參數是一個通 過 BigDecimal 定義的最 小值的字符串表示. |
沒有 |
@Digits(integer=, fraction= |
BigDecimal , BigInteger , String , byte , short , int , long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number and CharSequence . |
Checks whether the annoted value is a number having up to integer digits and fraction fractional digits |
對應的數據庫表 字段會被設置精度 (precision)和准度 (scale). |
@Future |
java.util.Date , java.util.Calendar ; Additionally supported by HV, if the Joda Time [http://joda- time.sourceforge.net/ ] date/time API is on the class path: any implementations of ReadablePartial and ReadableInstant . |
檢查給定的日期是否 比現在晚. |
沒有 |
@Max |
BigDecimal , BigInteger , byte , short , int , long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number . |
檢查該值是否大於或 等於約束條件中規定 的最大值 |
會給對應的數據庫表 字段添加一個check的 約束條件. |
@Min |
BigDecimal , BigInteger , byte , short , int , long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number . |
檢查該值是否大於或 等於約束條件中規定 的最小值. |
的最小值. 會給對應的數據庫表 字段添加一個check的 約束條件. |
@NotNull |
Any type |
Checks that the annotated value is not null. |
對應的表字段不允許 為null. |
@Null |
Any type |
Checks that the annotated value is null. |
沒有 |
@Past |
java.util.Date , java.util.Calendar ; Additionally supported by HV, if the Joda Time [http://joda- time.sourceforge.net/ ] date/time API is on the class path: any implementations of ReadablePartial and ReadableInstant . |
檢查標注對象中的值 表示的日期比當前早. |
沒有 |
@Pattern(regex=, flag= |
String . Additionally supported by HV: any sub-type of CharSequence |
檢查該字符串是否能 夠在 match 指定的情況 下被 regex 定義的正則 表達式匹配. |
沒有 |
@Size(min=, max=) |
String , Collection , Map and arrays . Additionally supported by HV: any sub-type of CharSequence . |
Checks if the annotated element's size is between min and max (inclusive). |
對應的數據庫表字段 的長度會被設置成約 束中定義的最大值. |
@Valid |
Any non-primitive type |
遞歸的對關聯對象進 行校驗, 如果關聯對 象是個集合或者數組, 那么對其中的元素進 行遞歸校驗,如果是一 個map,則對其中的值 部分進行校驗 |
沒有 |
注意: 這些約束都能支持message,groups,payload的參數 |
二,Additional constraints
除了Bean Validation API Hibernate Validator中的約束外,還有一些有用的定制的約束.其中@ScriptAssert是一個類級別的注釋
Annotation |
Supported data types |
作用 |
Hibernate metadata impact |
@CreditCardNumber |
CharSequence |
Checks that the annotated character sequence passes the Luhn checksum test. Note, this validation aims to check for user mistakes, not credit card validity! See also Anatomy of Credit Card Numbers [http:// www.merriampark.com/ anatomycc.htm]. |
沒有 |
@Email |
CharSequence |
Checks whether the specified character sequence is a valid email address. The optional parameters regexp and flags allow to specify an additional regular expression (including regular expression flags) which the email must match. |
沒有 |
@Length(min=, max=) |
CharSequence |
Validates that the annotated character sequence is between min and max included. |
對應的數據庫表字段 的長度會被設置成約 束中定義的最大值 |
@ModCheck(modType=, multiplier=, startIndex=, endIndex=, checkDigitPosition=, ignoreNonDigitCharacters=) |
CharSequence |
Checks that the digits within the annotated character sequence pass the mod 10 or mod 11 checksum algorithm. modType is used to select the modulo type and the multiplier determines the algorithm specific multiplier (see also Luhn algorithm [http:// en.wikipedia.org/ wiki/ Luhn_algorithm]). startIndex and endIndex allow to only run the modulo algorithm on the specified sub-string. checkDigitPosition allows to use an arbitrary digit within the character sequence to be the check digit. If not specified it is assumed that the check digit is part of the specified range. Last but not least, ignoreNonDigitCharacters allows to ignore non digit characters. |
沒有 |
@NotBlank |
CharSequence |
Checks that the annotated character sequence is not null and the trimmed length is greater than 0. The difference to @NotEmpty is that this constraint can only be applied on strings and that trailing whitespaces are ignored. |
沒有 |
@NotEmpty |
CharSequence , Collection , Map and arrays |
Checks whether the annotated element is not null nor empty |
沒有 |
@Range(min=, max=) |
BigDecimal , BigInteger , CharSequence , byte , short , int , long and the respective wrappers of the primitive types |
Checks whether the annotated value lies between (inclusive) the specified minimum and maximum. |
沒有 |
@SafeHtml(whitelistType=, additionalTags=) |
CharSequence |
Checks whether the annotated value contains potentially malicious fragments such as <script/ > . In order to use this constraint, the jsoup [http:// jsoup.org/] library must be part of the class path. With the whitelistType attribute predefined whitelist types can be chosen. You can also specify additional html tags for the whitelist with the additionalTags attribute. |
沒有 |
@ScriptAssert(lang=, script=, alias=) |
Any type |
Checks whether the given script can successfully be evaluated against the annotated element. In order to use this constraint, an implementation of the Java Scripting API as defined by JSR 223 ("Scripting for the Java TM Platform") must part of the class path. The expressions to be evaluated can be written in any scripting or expression language, for which a JSR 223 compatible engine can be found in the class path. |
沒有 |
@URL(protocol=, host=, port=, regexp=, flags=) |
CharSequence |
Checks if the annotated character sequence is a valid URL according to RFC2396. If any of the optional parameters protocol , host or port are specified, the corresponding URL fragments must match the specified values. The optional parameters regexp and flags allow to specify an additional regular expression (including regular expression flags) which the URL must match. |
沒有 |
三, Country specific constraints(略)