简体   繁体   中英

@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.

I'm looking at code that has @Valid on a method. 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? even if nothing in the contact has changed?

In Bean Validation, property-level annotations are generally placed on the 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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