Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top