简体   繁体   English

方法上的@Valid注释(Java EE 6)

[英]@Valid annotation on a method (Java EE 6)

If an object A has the @Valid annotation on a field or property, then that field or property, will also be validated when object A is validated. 如果对象A在字段或属性上具有@Valid批注,则在验证对象A时也将验证该字段或属性。

I'm looking at code that has @Valid on a method. 我正在查看在方法上具有@Valid的代码。 My first question is: is the assumption here that it is a getter for a property? 我的第一个问题是:这里是否假设它是财产的吸气剂?

@OneToOne
@Valid
public Contact getContact() {
    return contact;
}

So doing the above, equivalent to 这样做,相当于

@Valid Contact contact;

My second question is, the above annotation will always validate the contact object when Object A is validated? 我的第二个问题是,当验证对象A时,以上注释将始终验证联系人对象吗? even if nothing in the contact has changed? 即使联系人没有任何变化?

In Bean Validation, property-level annotations are generally placed on the getter. 在Bean验证中,属性级别的注释通常放在getter上。 The difference between annotating a field or the corresponding JavaBeans getter method is that in case of the latter the getter will be invoked by the validation engine to obtain the value, while in the first case the field value is accessed directly. 注释字段或相应的JavaBeans getter方法之间的区别在于,在后者的情况下,验证引擎将调用getter获取值,而在第一种情况下,直接访问字段值。 This can make a difference if your getter does any sort of additional calculation etc. 如果您的吸气剂进行了任何其他形式的计算,这可能会有所不同。

Regarding your second question, yes the associated contact will always be validated when validating the parent object, there is no check for changes or similar. 关于第二个问题,是的,在验证父对象时,关联的联系人将始终被验证,不检查更改或类似内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM