Property-level constraints

WARNING - TOPIC NOT WRITTEN - TOPIC ID: 2407

This topic has not yet been written. The content below is from the topic description.
2.1.2. Property-level constraints If your model class adheres to the JavaBeans standard, it is also possible to annotate the properties of a bean class instead of its fields. Example 2.2, “Property level constraint” uses the same entity as in Example 2.1, “Field level constraint”, however, property level constraints are used. Note The property's getter method has to be annotated, not its setter. Example 2.2. Property level constraint package com.mycompany; import javax.validation.constraints.AssertTrue; import javax.validation.constraints.NotNull; public class Car {     private String manufacturer;     private boolean isRegistered;            public Car(String manufacturer, boolean isRegistered) {         super();         this.manufacturer = manufacturer;         this.isRegistered = isRegistered;     }     @NotNull     public String getManufacturer() {         return manufacturer;     }     public void setManufacturer(String manufacturer) {         this.manufacturer = manufacturer;     }     @AssertTrue     public boolean isRegistered() {         return isRegistered;     }     public void setRegistered(boolean isRegistered) {         this.isRegistered = isRegistered;     } } When using property level constraints property access strategy is used to access the value to be validated. This means the bean validation provider accesses the state via the property accessor method. Tip It is recommended to stick either to field or property annotation within one class. It is not recommended to annotate a field and the accompanying getter method as this would cause the field to be validated twice.