内置的约束条件

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(略)

hibernate_validator_08的更多相关文章

随机推荐

  1. 我的VSTO之路(四):深入介绍Word开发

    原文:我的VSTO之路(四):深入介绍Word开发 在上一篇文章中,我介绍了Word的对象模型和一些基本开发技巧.为了更好的介绍Word插件开发,我为本文制作了一个Word书签的增强版,具体功能是让用 ...

  2. BZOJ 1083 [SCOI2005]繁忙的都市

    1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1664  Solved: 1080[Submit][Sta ...

  3. 实战weblogic集群之创建domain,AdminServer

    在weblogic安装完后,接下来就可以创建domain,AdminSever了. 第1步: $ cd /app/sinova/Oracle/wlserver_10./common/bin $ ./c ...

  4. 数据结构(树链剖分,线段树):SDOI 2016 游戏

    4515: [Sdoi2016]游戏 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 351  Solved: 157[Submit][Status][ ...

  5. Python中的迭代器和生成器

    本文以实例详解了python的迭代器与生成器,具体如下所示: 1. 迭代器概述: 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后 ...

  6. Linux 下让进程在后台可靠运行的几种方法

    想让进程在断开连接后依然保持运行?如果该进程已经开始运行了该如何补救? 如果有大量这类需求如何简化操作? 我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一 ...

  7. poj1003

    题目大意:超越悬挂 你可以把卡片最远堆到距离桌子多远?如果你有一张卡片,你可以堆卡片长度的一半如果有两张卡片那么可以堆1/2+1/3=5/6,如果有n张卡片那么可以堆1/2+1/3+1/4+..... ...

  8. 在eclipse下远程调试hadoop2.0

    在<在eclipse下编译hadoop2.0源码>一文中,我详细介绍了如何在eclipse环境和命令行环境下编译hadoop2.0源代码,并简单介绍了如何构建hadoop环境,这篇文章将着 ...

  9. tyvj1728 普通平衡树

    为了彻底理解树状数组,试着用树状数组做了下普通平衡树 而树状数组只能离线做,或者保证值的大小在数组可承受的范围内也是可以的,因为要求离线是因为必须事前对所有数离散化. 然后我们看刘汝佳蓝书上的图 利用 ...

  10. redis 实时从mysql 更新数据

    现在的互联网普遍都用redis+mysql ,查询一般放在 redis  上,更改 放在 redis  上, 如果更新 mysql数据到  redis 上呢? mysql 有四中类型的日志 Error ...