hibernate validator(一)

Hibernate Validator

Annotations are a very convenient and elegant way to specify invariant
constraints for a domain model. You can, for example, express that a
property should never be null, that the account balance should be strictly
positive, etc. These domain model constraints are declared in the bean
itself by annotating its properties. A validator can then read them and
check for constraint violations. The validation mechanism can be executed in
different layers in your application without having to duplicate any of
these rules (presentation layer, data access layer). Hibernate Validator has
been designed for that purpose.

注解是一种为领域模型(domain model)指定不变约束的简洁而幽雅的方法。例如,你能
表示一个属性永远不为null,一个帐户余额一定是正的,等等。这些域模型约束通过为bean中的属性添加
注解来加以声明。随后一个验证器(validator)会读取并检查这些约束。验证机制可以执行于应用程序中的
不同层(表现层、数据访问层),而不必复述任何(前述)这些规则。Hibernate Validator正为这一目的而设计的。

Hibernate Validator works at two levels. First, it is able to check
in-memory instances of a class for constraint violations. Second, it can
apply the constraints to the Hibernate metamodel and incorporate them into
the generated database schema.

Hibernate Validator工作在两个层次上。第一层,它能检查内存中一个类的实例是否违反约束。
第二层,它能将约束应用于Hibernate元模型上,并并将它们融入生成的数据库schema中。

Each constraint annotation is associated to a validator implementation
responsible for checking the constraint on the entity instance. A validator
can also (optionally) apply the constraint to the Hibernate metamodel,
allowing Hibernate to generate DDL that expresses the constraint. With the
appropriate event listener, you can execute the checking operation on
inserts and updates done by Hibernate. Hibernate Validator is not limited to
use with Hibernate. You can easily use it anywhere in your
application.

每个约束注解(constraint annotation)和一个验证器实现关联,该验证器负责检查位于实体实例上的约束一个验证
器也能(可选地)将 约束应用于Hibernate元模型上,让Hibernate生成表示这一约束的DDL。使用合适的事件监听器,你能
让Hibernate在插入和更新时执行检查操作。Hibernate Validator并不局限于同Hibernate一起使用。
你能在你应用程序的任何地方方便地使用它。

When checking instances at runtime, Hibernate Validator returns
information about constraint violations in an array of
InvalidValues. Among other information, the
InvalidValue contains an error description message
that can embed the parameter values bundle with the annotation (eg. length
limit), and message strings that may be externalized to a
ResourceBundle.

在运行时检查实例时,Hibernate Validator返回违反约束的信息,这些信息以一个InvalidValue数组的形式返回。
除了众多其他信息外,InvalidValue包含了一个错误描述消
息,该信息可以内嵌与注解相捆绑的参数值(例如长度限制),以及能被提取至ResourceBundle的消息字串。

Constraints

What is a constraint?

A constraint is represented by an annotation. A constraint usually
has some attributes used to parameterize the constraints limits. The
constraint apply to the annotated element.

约束通过注解表示。一个约束通常有一些用来参数化约束限制的属性。约束应用于带注解的元素。

Built in constraints

Hibernate Validator comes with some built-in constraints, which
covers most basic data checks. As we'll see later, you're not limited to
them, you can in a minute write your own constraints.

Hibernate Validator有些内建约束,这些约束覆盖了大多数的基本数据检查。随后我们会看到,
你不必受制于这些内置约束,因为一分钟内就可以写出你自己的约束。

内建约束

注解应用目标运行时检查Hibernate元数据效果
@Length(min=, max=)property (String)check if the string length match the rangeColumn length will be set to max
@Length(min=, max=)property (String)检查字符串长度是否符合范围列长度会被设到最大值
@Max(value=)property (numeric or string representation of a numeric)check if the value is less than or equals to maxAdd a check constraint on the column
@Max(value=)property (numeric or string representation of a numeric)检查值是否小于或等于最大值对列增加一个检查约束
@Min(value=)property (numeric or string representation of a numeric)check if the value is more than or equals to minAdd a check constraint on the column
@Min(value=)property (numeric or string representation of a numeric)检查值是否大于或等于最小值对列增加一个检查约束
@NotNullpropertycheck if the value is not nullColumn(s) are not null
@NotNullproperty检查值是否非空(not null)列不为空
@Pastproperty (date or calendar)check if the date is in the pastAdd a check constraint on the column
@Pastproperty (date or calendar)检查日期是否是过去时对列增加一个检查约束
@Futureproperty (date or calendar)check if the date is in the futurenone
@Futureproperty (date or calendar)检查日期是否是将来时none
@Pattern(regex="regexp", flag=)property (string)check if the property match the regular expression given a match flag (see java.util.regex.Pattern )none
@Pattern(regex="regexp", flag=)property (string)检查属性是否与给定匹配标志的正则表达式相匹配(见 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
@Range(min=, max=)property (numeric or string representation of a numeric)检查值是否在最小和最大值之间(包括临界值)对列增加一个检查约束
@Size(min=, max=)property (array, collection, map)check if the element size is between min and max(included)none
@Size(min=, max=)property (array, collection, map)检查元素大小是否在最小和最大值之间(包括临界值)none
@AssertFalsepropertycheck that the method evaluates to false (useful forconstraints expressed in code rather than annotations)none
@AssertFalseproperty检查方法的演算结果是否为false(对以代码方式而不是注解表示的约束很有用)none
@AssertTruepropertycheck that the method evaluates to true (useful for constraints expressed in code rather than annotations)none
@AssertTrueproperty检查方法的演算结果是否为true(对以代码方式而不是注解表示的约束很有用)none
@Validproperty (object)Perform validation recursively on the associatedobjectnone
@Validproperty (object)对关联对象递归的进行验证none

Error messages

Hibernate Validator comes with a default set of error messages
translated in a few languages (if yours is not part of it, please sent
us a patch). You can override those messages by creating a
ValidatorMessages.properties or
(ValidatorMessages_loc.properties) out of
org.hibernate.validator.resources.DefaultValidatorMessages.properties
and change the appropriate keys. You can even add your own additional
set of messages while writing your validator annotations.

Hibernate Validator提供了一组默认的错误提示信息,它们被翻译成多种语言(如果你的语言不在其中,请给
我们寄一个补丁)。你可以在org.hibernate.validator.resources.DefaultValidatorMessages.properties
之外创建ValidatorMessages.propertiesValidatorMessages_loc.properties
文件并改变相应的键值,籍此覆盖那些(默认)信息。你甚至可以在写自己的验证器
注解时添加你自己的附加消息集。

Alternatively you can provide a
ResourceBundle while checking programmatically
the validation rules on a bean.

或者你可以以编程方式检查bean的验证规则并提供相应的 ResourceBundle

Writing your own constraints

Extending the set of built-in constraints is extremely easy. Any
constraint consists of two pieces: the constraint
descriptor (the annotation) and the constraint
validator (the implementation class). Here is a
simple user-defined descriptor:

扩展内建约束集是极其方便的。任何约束都包括两部分:约束描述符(注解)
和约束验证器(实现类)。下面是一个简单的用户定义描述符:

programlisting

@ValidatorClass(CapitalizedValidator.class)
@Target(METHOD)
@Retention(RUNTIME)
@Documented
public @interface Capitalized {
CapitalizeType type default Capitalize.FIRST;
String message() default "has incorrect capitalization";
}

type is a parameter describing how the property
should to be capitalized. This is a user parameter fully dependant on
the annotation business.

type参数描述属性应该如何被大写。这是一个完全依赖于注解业务(逻辑)的用户
参数。

message is the default string used to describe
the constraint violation and is mandatory. You can hard code the string
or you can externalize part/all of it through the Java ResourceBundle
mechanism. Parameters values are going to be injected inside the message
when the {parameter} string is found (in our example
Capitalization is not {type} would generate
Capitalization is not FIRST), externalizing the whole
string in ValidatorMessages.properties is
considered good practice. See .

message是用于描述约束违规的默认字符串,它是强制要求的。你可以采取硬编码的方式,
或者通过Java ResourceBundle机制将message的部分/全部内容提取至外部文件。一旦发现message中{parameter}字符串,
就会在{parameter}这个位置注入相应的参数值(在我们的例子里Capitalization is not {type}会生成 Capitalization is not FIRST),
可以将message对应的整个字符串提取至外部文件ValidatorMessages.properties,这也是一种良好实践。
见。

programlisting

@ValidatorClass(CapitalizedValidator.class)
@Target(METHOD)
@Retention(RUNTIME)
@Documented
public @interface Capitalized {
CapitalizeType type default Capitalize.FIRST;
String message() default "{validator.capitalized}";
}

...
#in ValidatorMessages.properties
validator.capitalized=Capitalization is not {type}

As you can see the {} notation is recursive.

如你所见{}符号是递归的。

To link a descriptor to its validator implementation, we use the
@ValidatorClass meta-annotation. The validator class
parameter must name a class which implements
Validator<ConstraintAnnotation>.

为了将一个描述符连接到它的验证器实现,我们使用@ValidatorClass
元注解。验证器类参数必须指定一个实现了Validator<ConstraintAnnotation>
的类。

We now have to implement the validator (ie. the rule checking
implementation). A validation implementation can check the value of the
a property (by implementing PropertyConstraint)
and/or can modify the hibernate mapping metadata to express the
constraint at the database level (by implementing
PersistentClassConstraint).

我们现在要实现验证器(也就是实现规则检查)。一个验证器实现能检查一个属性的值
(实现PropertyConstraint),并且/或者可以修改hibernate映射元数据
(实现PersistentClassConstraint),籍此表示数据库级的约束。

programlisting

public class LengthValidator
implements Validator<Capitalized>, PropertyConstraint {
private CapitalizeType type;

//part of the Validator<Annotation> contract,
//allows to get and use the annotation values
public void initialize(Capitalized parameters) {
type = parameters.type();
}

//part of the property constraint contract
public boolean isValid(Object value) {
if (value==null) return true;
if ( !(value instanceof String) ) return false;
String string = (String) value;
if (type == CapitalizeType.ALL) {
return string.equals( string.toUpperCase() );
}
else {
String first = string.substring(0,1);
return first.equals( first.toUpperCase();
}
}
}

The isValid() method should return false if the
constraint has been violated. For more examples, refer to the built-in
validator implementations.

如果违反约束,isValid()方法将返回false。更多例子请参考内建验证器实现。

We only have seen property level validation, but you can write a
Bean level validation annotation. Instead of receiving the return
instance of a property, the bean itself will be passed to the validator.
To activate the validation checking, just annotated the bean itself
instead. A small sample can be found in the unit test suite.

至此我们只看到属性级的验证,你还可以写一个Bean级别的验证注解。Bean自身会被传递给验证器,
而不是bean的属性实例。只要对bean自身进行注解即可激活验证检查。在单元测试套件中还可以找到一个小例子。

Annotating your domain model

Since you are already familiar with annotations now, the syntax
should be very familiar.

既然你现在已经熟悉注解了,那么对语法也应该很清楚了。

programlisting

public class Address {
private String line1;
private String line2;
private String zip;
private String state;
private String country;
private long id;

// a not null string of 20 characters maximum
@Length(max=20)
@NotNull
public String getCountry() {
return country;
}

// a non null string
@NotNull
public String getLine1() {
return line1;
}

//no constraint
public String getLine2() {
return line2;
}

// a not null string of 3 characters maximum
@Length(max=3) @NotNull
public String getState() {
return state;
}

// a not null numeric string of 5 characters maximum
// if the string is longer, the message will
//be searched in the resource bundle at key 'long'
@Length(max=5, message="{long}")
@Pattern(regex="[0-9]+")
@NotNull
public String getZip() {
return zip;
}

// should always be true
@AssertTrue
public boolean isValid() {
return true;
}

// a numeric between 1 and 2000
@Id @Min(1)
@Range(max=2000)
public long getId() {
return id;
}
}

While the example only shows public property validation, you can
also annotate fields of any kind of visibility.

上面的例子只展示了公共属性验证,你还可以对任何可见度的字段(field)进行注解。

programlisting

@MyBeanConstraint(max=45)
public class Dog {
@AssertTrue private boolean isMale;
@NotNull protected String getName() { ... };
...
}

You can also annotate interfaces. Hibernate Validator will check
all superclasses and interfaces extended or implemented by a given bean
to read the appropriate validator annotations.

你可以对接口进行注解。Hibernate Validator会检查给定bean所扩展或实现的所有超类和接口,
籍以读取相应的验证器注解(信息)。

programlisting

public interface Named {
@NotNull String getName();
...
}

public class Dog implements Named {

@AssertTrue private boolean isMale;

public String getName() { ... };

}

The name property will be checked for nullity when the Dog bean is
validated.

在验证Dog bean时会检查name属性的无效性(是否为null)。

Using the Validator framework

Hibernate Validator is intended to be used to implement
multi-layered data validation, where we express constraints in one place
(the annotated domain model) and apply them at various different layers of
the application.

Hibernate Validator旨在实现多层数据验证,我们在一处表示约束(带注解的域模型),然后将其运用于
应用程序的不同层。

Database schema-level validation

Out of the box, Hibernate Annotations will translate the
constraints you have defined for your entities into mapping metadata.
For example, if a property of your entity is annotated
@NotNull, its columns will be declared as
not null in the DDL schema generated by
Hibernate.

无须额外手续,Hibernate Annotations会把你为实体定义的约束自动翻译为映射元数据。例如,如果你的实体
的一个属性注解为@NotNull,在Hibernate生成的DDL模式中这列会被定义为
not null

Hibernate event-based validation

Hibernate Validator has two built-in Hibernate event listeners.
Whenever a PreInsertEvent or
PreUpdateEvent occurs, the listeners will verify all
constraints of the entity instance and throw an exception if any
constraint is violated. Basically, objects will be checked before any
inserts and before any updates made by Hibernate. This is the most
convenient and the easiest way to activate the validation process. On
constraint violation, the event will raise a runtime
InvalidStateException which contains an array of
InvalidValues describing each failure.

Hibernate Validator有两个内建Hibernate事件监听器。当一个PreInsertEvent
PreUpdateEvent发生时,监听器会验证该实体实例的所有约束,如有违反会抛出一个异常。
基本上,在Hibernate执行任何插入和更新前对象会被检查。这是激活验证过程的最便捷最简单的方法。当遇到约束
违规时,事件会引发一个运行时InvalidStateException,该异常包含一个描述每个错误的
InvalidValue数组。

programlisting

<hibernate-configuration>
...
<event type="pre-update">
<listener
class="org.hibernate.validator.event.ValidatePreUpdateEventListener"/>
</event>
<event type="pre-insert">
<listener
class="org.hibernate.validator.event.ValidatePreInsertEventListener"/>
</event>
</hibernate-configuration>

note

When using Hibernate Entity Manager, the Validation framework
is activated out of the box. If the beans are not annotated with
validation annotations, there is no performance cost.

note

在使用Hibernate Entity Manager时,Validation框架会被自动激活。如果bean不带验证注解,
不会有性能损失。

Application-level validation

Hibernate Validator can be applied anywhere in your application
code.

Hibernate Validator能应用于你应用程序代码的任何地方。

programlisting

ClassValidator personValidator = new ClassValidator( Person.class );
ClassValidator addressValidator = new ClassValidator( Address.class, ResourceBundle.getBundle("messages", Locale.ENGLISH) );

InvalidValue[] validationMessages = addressValidator.getInvalidValues(address);

The first two lines prepare the Hibernate Validator for class
checking. The first one relies upon the error messages embedded in
Hibernate Validator (see ), the second one uses a
resource bundle for these messages. It is considered a good practice to
execute these lines once and cache the validator instances.

头两行为执行类检查而准备Hibernate Validator。第一行依赖于嵌入在Hibernate Validator内的错误
消息(见),第二行为这些消息准备资源包。这些代码只执行一次,
并将验证器进行缓存处理,这种方式是一种良好实践。

The third line actually validates the Address
instance and returns an array of InvalidValues. Your
application logic will then be able to react to the failure.

第三行真正验证了Address实例并返回一个InvalidValue数组。
你的应用程序逻辑随后可以对错误做出响应。

You can also check a particular property instead of the whole
bean. This might be useful for property per property user
interaction

除了针对整个bean你还可以对某个特定属性进行检查。这对于一个属性一个属性的用户交互情形或许是有用的。

programlisting

ClassValidator addressValidator = new ClassValidator( Address.class, ResourceBundle.getBundle("messages", Locale.ENGLISH) );

//only get city property invalid values
InvalidValue[] validationMessages = addressValidator.getInvalidValues(address, "city");

//only get potential city property invalid values
InvalidValue[] validationMessages = addressValidator.getPotentialInvalidValues("city", "Paris")

Validation informations

As a validation information carrier, hibernate provide an array of
InvalidValue. Each
InvalidValue has a buch of methods describing the
individual issues.

作为一个验证信息的载体,hibernate提供了一个InvalidValue数组。
每个InvalidValue有一组,这些方法分别描述相应的个体问题。

getBeanClass() retrieves the failing bean
type

getBeanClass()获取失败的bean类型。

getBean()retrieves the failing instance?
if any ie not when using?
getPotentianInvalidValues())

getBean()获取验证失败的实例(如果有的话,当使用
getPotentianInvalidValues()时则不会取到)

getValue() retrieves the failing value

getValue()获取验证失败的值

getMessage() retrieves the proper
internationalized error message

getMessage()获取合适的国际化错误消息

getRootBean() retrieves the root bean
instance generating the issue (useful in conjunction with
@Valid), is null if getPotentianInvalidValues() is
used.

getRootBean()获取产生问题的根bean实例(在与@Valid连用
时很有用),如用getPotentianInvalidValues()则返回null。

getPropertyPath() retrieves the dotted path of
the failing property starting from the root bean

getPropertyPath()获取"问题"属性从根bean开始的带点的路径

<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator" dc:identifier="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator" dc:title="Hibernate Validator" trackback:ping="http://wiki.redsaga.com/confluence/rpc/trackback/126" /> </rdf:RDF> --><!-- Root decorator: all decisions about how a page is to be decorated via the inline decoration begins here. --><!-- Switch based upon the context. However, for now, just delegate to a decorator identified directly by the context. -->

<!-- comments should always display underneath the content. we should have a 'clear:both' here just in case there are floats or aligned images in the content -->

review完毕,结果如下: 

1。Annotations are a very convenient and elegant way to specify invariant constraints for a domain model.
1。注解是一种为领域模型指定不变约束的简介而幽雅的方法

2。These domain model constraints are declared in the bean itself by annotating its properties.
2。......通过为bean中的属性添加注解来加以声明(原文的"注解属性"容易引起歧义)

3。The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules
3。验证机制可以执行于应用程序中的不同层(表现层、数据访问层),而不必复述任何(前述)这些规则

4。incorporate them into the generated database schema.
4。并将它们融入生成的数据库schema中

5。each constraint annotation is associated to a validator implementation responsible for checking the constraint on the entity instance.
5。每个约束注解(constraint annotation)关联于一个验证器实现,该验证器负责检查位于实体实例上的约束

6。Among other information, ...... can embed the parameter values bundle with the annotation......, and message strings that may be externalized to ......
除了众多其他信息外,......该信息可以内嵌与注解相捆绑的参数值(例如长度限制),以及能被提取至ResourceBundle的消息字串

7。check if the value is not null......check if the property match the regular expression given a match flag......check if the value is between min and max(included)
检查值是否为非空(not null)......检查属性是否与给定匹配标志的正则表达式相匹配......(包含最大值)

8。check that the method evaluates to false......check that the method evaluates to true
8。检查方法的演算结果是否为false......检查方法的演算结果是否为true

9。Perform validation recursively on the associated object
9。对关联对象递归的进行验证

10。Hibernate Validator comes with a default set of error messages translated in a few languages
10。Hibernate Validator提供了一组默认的错误提示信息,它们被翻译成多种语言

11。You can override those messages by creating a......
11。你可以在org.hibernate.validator.resources.DefaultValidatorMessages.properties之外创建ValidatorMessages.properties或ValidatorMessages_loc.properties文件并改变相应的键值,籍此覆盖那些(默认)信息

12。Alternatively you can provide a......while checking programmatically the validation rules on a bean.
12。或者,你可以在以编程方式检查bean的验证规则时提供一个ResourceBundle。

13。Here is a simple user-defined descriptor
13。用户自定义

14。......is a parameter describing how the property should to be capitalized. This is a user parameter fully dependant on the annotation business.
14。type参数描述属性应该如何被大写。这是一个完全依赖于注解业务(逻辑)的用户参数。

15。You can hard code...... through the Java ResourceBundle mechanism.
15。或者通过Java ResourceBundle机制将部分/全部内容提取出来

16。externalizing the whole string in <filename>ValidatorMessages.properties</filename> is considered good practice.
16。将完整字串提取至ValidatorMessages.properties被认为是一种好的实践。

17。We only have seen property level validation......
17。......你还可以写一个......

18。the syntax should......
18。语法

19。Hibernate Validator will check all superclasses and interfaces extended or implemented by a given bean to read the appropriate validator annotations.
19。Hibernate Validator会检查给定bean扩展或实现的所有超类和接口,以读取相应的验证器注解(信息)

20。The name property will be checked for nullity......
20。......检查name属性是否为空值......

21。where we express constraints in one place (the annotated domain model) and apply them at various different layers of the application.
21。我们在一处表达约束(带注解的领域模型),然后将之运用于应用程序的不同层

22。Out of the box, ......
22。无须额外手续,Hibernate Annotations会将你为实体定义的......自动翻译成......

23。This is the most convenient and the easiest way to activate the validation process.
23。这是激活验证过程最便捷最简单的途径

24。When using Hibernate Entity Manager, the Validation framework is activated out of the box
24。......验证框架会被自动激活......(一个疑问:既然Hibernate Annotation做了翻译,是否Hibernate Entity Manager也需要翻译呢?个人觉得Hibernate Annotation作为一个特定的framework实现,其意义已经有别于作为一般概念的Annotation了,因此,或许,也可不用翻译)

25。the second one uses a resource bundle for these messages
25。......消息......(原译文此处是别字)

26。This might be useful for property per property user interaction
26。这对于一个属性一个属性的用户交互情形或许是有用的

27。has a buch of methods describing the individual issues.
27。......一组方法分别描述各个问题

28。if any ie not when using <methodname>getPotentianInvalidValues()</methodname>
28。......如果有的话,当使用getPotentianInvalidValues()时则不会取到。(此处不能确信,最好查一下getPotentianInvalidValues的使用,另,ie must be -- that is)

29。retrieves the root bean instance generating the issue (useful in conjunction with <literal>@Valid</literal>), is null if getPotentianInvalidValues() is used.
29。获得产生问题的根bean实例......如果使用getPotentianInvalidValues()则返回null

30。 retrieves the dotted path of the failing property starting from the root bean
30。获得"问题"属性从根bean开始的带点的路径

Posted by Mo Ying at Mar 12, 2006 19:59 | Permalink
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=153#comment-153" dc:identifier="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=153#comment-153" dc:title="$comment.title" trackback:ping="http://wiki.redsaga.com/confluence/rpc/trackback/153" /> </rdf:RDF> -->

感谢mo ying的修改意见,我已经将一审内容合并好放在attachment里了。 除了1、13、20都按照mo ying的意见改了。

=========================================

1。Annotations are a very convenient and elegant way to specify invariant constraints for a domain model.
1。注解是一种为领域模型指定不变约束的简介而幽雅的方法

此处"简介"是否该是"简洁"?

7。check if the value is not null......check if the property match the regular expression given a match flag......check if the value is between min and max(included)
检查值是否为非空(not null)......检查属性是否与给定匹配标志的正则表达式相匹配......(包含最大值)

included指的是min and max还是单指max?

================================================

3。The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules
3。验证机制可以执行于应用程序中的不同层(表现层、数据访问层),而不必复述任何(前述)这些规则

5。each constraint annotation is associated to a validator implementation responsible for checking the constraint on the entity instance.
5。每个约束注解(constraint annotation)关联于一个验证器实现,该验证器负责检查位于实体实例上的约束

3和5我在翻译时倾向于将修饰成分插入句中,没有拆成另外一句,问一下,以后翻译碰到类似情况怎么通顺怎么翻译还是根据原文的分割决定?

13。Here is a simple user-defined descriptor
13。用户自定义

这里似乎怎么看都该是用户自定义描述符

20。The name property will be checked for nullity......
20。......检查name属性是否为空值......

我觉得此处的nullity似乎还是该翻为无效性比较合适,当然意思上没有什么区别,不过忠于原文而已。

============================================

17。We only have seen property level validation......
17。......你还可以写一个......
连漏了个"还"都看出来了,mo ying的review真是认真啊

24。When using Hibernate Entity Manager, the Validation framework is activated out of the box
24。......验证框架会被自动激活......(一个疑问:既然Hibernate Annotation做了翻译,是否Hibernate Entity Manager也需要翻译呢?个人觉得Hibernate Annotation作为一个特定的framework实现,其意义已经有别于作为一般概念的Annotation了,因此,或许,也可不用翻译)
之前没有注意到Validation的V是大写的,应该是指特定的framework,此处Validation不用翻译

Posted by DigitalSonic at Mar 13, 2006 13:27 | Permalink
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=175#comment-175" dc:identifier="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=175#comment-175" dc:title="$comment.title" trackback:ping="http://wiki.redsaga.com/confluence/rpc/trackback/175" /> </rdf:RDF> -->

1。确实是"简洁",my fault

7。这个当时确实没留意。一般讲min/max或者range时,首先都会包含min,而有时往往对于max到有可能是exclude的(即:min<=?<max),于是我在review时想,作者在此处加上included,大概是为了明确并非通常情况下的"<max",而是"<=max"。不过根据前面的"between",或许翻译成"包含(临界值)"更恰当。

3。5。我的理解是这样子的,通顺是首要的,英语语法毕竟和中文不同,很多时候对于长句,在翻译时将之拆分成短句似乎是常用手法,因为过多定语很容易让人产生理解上的困难,但如果能保持原来句子结构又依然保持通顺,那自然是最好,但这一般比较有难度。

13。没错,我忘了后面加"......"了,呵呵

20。也可以。nullity原意是无效性,而此处结合上下文就是"检查name属性是不是非空",当时这么改主要是因为诈一看"无效性"似乎不能马上联想到"检查not null"(因为无效性可能有多种情况),不过这个似乎也问题不大。

24。赞同

Posted by Mo Ying at Mar 13, 2006 14:34 | Permalink
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=176#comment-176" dc:identifier="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=176#comment-176" dc:title="$comment.title" trackback:ping="http://wiki.redsaga.com/confluence/rpc/trackback/176" /> </rdf:RDF> -->

You can also annotate interfaces

你还能注解接口

还可以对接口进行注解(感觉原来的语气有点生硬 )

Posted by 菠菜 at Mar 14, 2006 17:52 | Permalink
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=182#comment-182" dc:identifier="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=182#comment-182" dc:title="$comment.title" trackback:ping="http://wiki.redsaga.com/confluence/rpc/trackback/182" /> </rdf:RDF> -->

You can also annotate interfaces

你还能注解接口

还可以对接口进行注解(感觉原来的语气有点生硬 )

----这个修改放到二审里吧,免得改来改去的,到时一起合并。

Posted by DigitalSonic at Mar 16, 2006 09:18 | Permalink
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=188#comment-188" dc:identifier="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=188#comment-188" dc:title="$comment.title" trackback:ping="http://wiki.redsaga.com/confluence/rpc/trackback/188" /> </rdf:RDF> -->

1.
Annotations are a very convenient and elegant way to specify invariant constraints for a domain model.
注解是一种为领域模型(domain model)指定不变约束的简洁而幽雅的方法。
->
注解通过一种简洁而幽雅的方法为领域模型(domain model)指定不变约束。
2.
Each constraint annotation is associated to a validator implementation
每个约束注解(constraint annotation)关联于一个验证器实现
->
每个约束注解(constraint annotation)和一个验证器实现关联

3.
When checking instances at runtime, Hibernate Validator returns information about constraint violations in an array of <classname>InvalidValue</classname>s
在运行时检查实例时,Hibernate Validator以一个<classname>InvalidValue</classname>数组来返回违反约束的信息。
->
在运行时检查实例时,Hibernate Validator返回违反约束的信息,这些信息以一个<classname>InvalidValue</classname>数组的形式返回。

4.
build-in
内建
->内置(纯属个人偏好,可以不予理会。_。)

5
Hibernate Validator comes with some built-in constraints, which covers most basic data checks. As we'll see later, you're not limited to them, you can in a minute write your own constraints
Hibernate Validator有些内建约束,覆盖了大多数的基本数据检查。正如我们随后要看到的,你并不局限于它们,你能在一分钟内写出你自己的约束。
->
Hibernate Validator有些内置约束,这些约束覆盖了大多数的基本数据检查。随后我们会看到,你不必受制于这些内置约束,因为一分钟内就可以写出你自己的约束。

6
<table><title>Built-in constraints</title>...</table>中的和property有关的部分没有翻译

7
<thead>
<row>
<entry>Annotation</entry>
<entry>Apply on</entry>
<entry>Runtime checking</entry>
<entry>Hibernate Metadata impact</entry>
</row>
</thead>
这部分没有翻译
->

注解应用目标运行时检查Hibernate元数据效果

8
<row>
<entry>@NotNull</entry>
<entry>property</entry>
<entry>check if the value is not null</entry>
<entry>Column(s) are not null</entry>
</row>
<row>
<entry>@NotNull</entry>
<entry>property</entry>
<entry>检查值是否为非空</entry>
<entry>列不为null</entry>
</row>

检查值是否为非空
->
检查指是否非空(null)

列不为null
->
列不为空(null)

9
check if the date is in the past
检查日期是否是过去的
->
过去时

check if the date is in the future
检查日期是否是将来的
->
将来时

10
Alternatively you can provide a <classname>ResourceBundle</classname> while checking programmatically the validation rules on a bean.
或者,你可以在以编程方式检查bean的验证规则时提供一个<classname> ResourceBundle</classname>。
->
或者以编程方式检查bean的验证规则并提供相应的<classname> ResourceBundle</classname>,

11
Here is a simple user-defined descriptor:
以下是一个简单的用户定义描述符:
->
下面是一个简单的用户定义描述符:

12
You can hard code the string or you can externalize part/all of it through the Java ResourceBundle mechanism.
你可以硬编码字符串或者通过Java ResourceBundle机制将部分/全部内容提取出来。
->
你可以采取硬编码的方式,或者通过Java ResourceBundle机制将message的部分/全部内容提取至外部文件。

13
Parameters values are going to be injected inside the message when the <literal> {parameter} string is found (in our example Capitalization is not {type} would generate Capitalization is not FIRST), externalizing the whole string in ValidatorMessages.properties is considered good practice.
参数值将在{parameter}字符串被发现时被注入到消息内(在我们的例子里Capitalization is not {type}会生成 Capitalization is not FIRST),将完整字串提取至 ValidatorMessages.properties被认为是一种好的实践。
->
一旦发现message中{parameter}字符串,就会在{parameter}这个位置注入相应的参数值(例如:Capitalization is not {type}会生成 Capitalization is not FIRST)。可以将message对应的整个字符串提取至外部文件ValidatorMessages.properties,这也是一种良好实践。

这段文字好像被wiki破坏了,大概是wiki macro的原因.

14
We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing <literal>PropertyConstraint</literal>) and/or can modify the hibernate mapping metadata to express the constraint at the database level (by implementing <literal>PersistentClassConstraint</literal>).
我们现在要实现验证器(就是规则检查实现)。一个验证器实现能检查一个属性的值(通过实现<literal>PropertyConstraint</literal>)并且/或者可以修改hibernate映射元数据来表示数据库级的约束(通过实现<literal>PersistentClassConstraint</literal>)。
->
我们现在要实现验证器(也就是实现规则检查)。一个验证器实现能检查一个属性的值(实现<literal>PropertyConstraint</literal>),并且/或者可以修改hibernate映射元数据(实现<literal>PersistentClassConstraint</literal>),籍此表示数据库级的约束。

15
The <literal>isValid()</literal> method should return false if the constraint has been violated.
如果约束被违反了,<literal>isValid()</literal>方法应该返回 false。
->
如果违反约束,<literal>isValid()</literal>方法将返回false。

16
We only have seen property level validation, but you can write a Bean level validation annotation. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just annotated the bean itself instead. A small sample can be found in the unit test suite.
我们只看到了属性级的验证,你还可以写一个Bean级别的验证注解。bean本身会被传递给验证器,而不是接收返回的属性实例。要激活验证检查,就只要注解bean本身就可以了。在单元测试套件中可以找到一个小的例子。
->
至此我们只看到属性级的验证,你还可以写一个Bean级别的验证注解。Bean自身会被传递给验证器,而不是bean的属性实例。只要对bean自身进行注解即可激活验证检查。在单元测试套件中还可以找到一个小例子。

17
Since you are already familiar with annotations now, the syntax should be very familiar.
既然你现在已经熟悉注解了,那么语法应该很清楚了。
->
既然你现在已经熟悉注解了,那么对语法也应该很清楚了。

18
While the example only shows public property validation, you can also annotate fields of any kind of visibility.
例子只展示了公共属性验证,你还能注解任何可见度的字段(field)。
->
上面的例子只展示了公共属性验证,你还可以对任何可见度的字段(field)进行注解。

19
You can also annotate interfaces. Hibernate Validator will check all superclasses and interfaces extended or implemented by a given bean to read the appropriate validator annotations.
你还能注解接口。Hibernate Validator会检查给定bean扩展或实现的所有超类和接口,以读取相应的验证器注解(信息)。
->
你可以对接口进行注解。Hibernate Validator会检查给定bean所扩展或实现的所有超类和接口,籍以读取相应的验证器注解(信息)。

20
where we express constraints in one place (the annotated domain model) and apply them at various different layers of the application.
我们在一处表示约束(带注解的域模型),然后将之运用于应用程序的不同层。
->
我们在一处表示约束(带注解的域模型),然后将运用于应用程序的不同层。

21
On constraint violation, the event will raise a runtime <classname>InvalidStateException</classname> which contains an array of <literal>InvalidValue</literal>s describing each failure.
当遇到约束违规时,事件会引发一个运行时<classname>InvalidStateException</classname>,它包含一个描述每个错误的<literal>InvalidValue</literal>数组。
->
当遇到约束违规时,事件会引发一个运行时<classname>InvalidStateException</classname>,该异常包含一个描述每个错误的<literal>InvalidValue</literal>数组。

22
When using Hibernate Entity Manager, the Validation framework is activated out of the box.
在使用Hibernate Entity Manager时,Validation框架会在外部被自动激活。
->
在使用Hibernate Entity Manager时,Validation框架会被自动激活。

23
It is considered a good practice to execute these lines once and cache the validator instances.
执行一次这些代码并缓存验证器实例是一个好的实践。
->
这些代码只执行一次,并将验证器进行缓存处理,这种方式是一种良好实践。

24
Your application logic will then be able to react to the failure.
你的应用程序逻辑随后可以对错误做出反应。
->
你的应用程序逻辑随后可以对错误做出响应

25
You can also check a particular property instead of the whole bean.
除了针对整个bean以外你还可以对特定一个属性进行检查。
->
除了针对整个bean你还可以对某个特定属性进行检查。

26
property per property
一个属性一个属性
->?

27
Each <literal>InvalidValue</literal> has a buch of methods describing the individual issues.
每个<literal>InvalidValue</literal>有一组方法分别描述各个问题。
->
每个<literal>InvalidValue</literal>有一组方法,这些方法分别描述相应的个体问题。

28
Validation informations这个部分
retrieves 获得 -> 获取
failing 失败 ->验证失败(这样翻译信息比较全面一点)

Posted by 菠菜 at Mar 28, 2006 09:25 | Permalink
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=254#comment-254" dc:identifier="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=254#comment-254" dc:title="$comment.title" trackback:ping="http://wiki.redsaga.com/confluence/rpc/trackback/254" /> </rdf:RDF> -->

1.
Annotations are a very convenient and elegant way to specify invariant constraints for a domain model.
注解是一种为领域模型(domain model)指定不变约束的简洁而幽雅的方法。
->
注解通过一种简洁而幽雅的方法为领域模型(domain model)指定不变约束。

我觉得这里的句子主干是Annotations are a way所以译文对应的也该是注解是一种方法。

4.
build-in
内建
->内置(纯属个人偏好,可以不予理会。_。)

那就不予理会了

10
Alternatively you can provide a <classname>ResourceBundle</classname> while checking programmatically the validation rules on a bean.
或者,你可以在以编程方式检查bean的验证规则时提供一个<classname> ResourceBundle</classname>。
->
或者以编程方式检查bean的验证规则并提供相应的<classname> ResourceBundle</classname>,

或者你可以以编程方式检查bean的验证规则并提供相应的<classname> ResourceBundle</classname>。

26
property per property
一个属性一个属性
->?

有什么问题?一个属性对应一个属性的?这里没有修改.

Posted by DigitalSonic at Mar 28, 2006 11:16 | Permalink
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=255#comment-255" dc:identifier="http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator?focusedCommentId=255#comment-255" dc:title="$comment.title" trackback:ping="http://wiki.redsaga.com/confluence/rpc/trackback/255" /> </rdf:RDF> -->

1.
原文也没有问题,不过句子太长了,有点拗口,所以拆分了一下

26.
没有问题,只是想征求一下有没有更好的中文翻译

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值