A property will be validated on client side when its value is changed, and it will be validated again when its value is set during model update on server side if its validation is not delayed. Entity-level validation is called in Invoke Application phase.
@Column(nullable=false)
public String getName() {
return name;
}
@JoinColumn(nullable=true, name="addressId")
public Address getAddress() {
return address;
}
A property is required if it is not nullable.
JPA annotations can be overridden by property annotations. For example,
@Property(name="address", view={ViewType.ALL},
required=RequiredType.REQUIRED)
The property is required even if it is nullable in persistence.
Property annotations can be overridden by ViewConfig propertyDescriptor. For example,
EntityViewConfig viewConfig = new EntityViewConfig(ViewType.ENTITY);
PropertyDescriptor pd = viewConfig.getPropertyDescriptor("address", true, false);
pd.setRequired(true);
ViewConfig overrides property annotations that override JPA annotations.
@Column(nullable=false, length=20)
public String getName() {
return name;
}
The maximum length is 20, and minimum length is 1 (not nullable).
JPA annotations can be overridden by property annotations. For example,
@javax.persistence.Lob
@Column(nullable=false, length=65536)
public String getDescription() {
return description;
}
@Property(name="description", minLength=10, maxLength=1024)
The length of description is between 10 and 1024 characters even though
it is annotated to be 64K.
@Property(name="size", minValue=100, maxValue=500)
The property value must be in the range from 100 to 500, inclusive.
@Property(name="email", view={ViewType.ALL},
pattern="^[a-zA-Z0-9.\\-_]+@[a-zA-Z0-9.\\-_]+\\.[a-zA-Z]{2,10}$")
Pattern is a regular expression. The property value must match the pattern to
be valid.
Property annotations can be overridden by ViewConfig propertyDescriptor. For example,
EntityViewConfig viewConfig = new EntityViewConfig(ViewType.ENTITY);
PropertyDescriptor pd = viewConfig.getPropertyDescriptor("email", true, false);
pd.setPattern("a pattern");
@UniqueKeys({
@UniqueKey(properties={"id"}),
@UniqueKey(properties={"name", "phone"})
})
@Entity(name="Employee")
public class Employee extends Person {
}
@UniqueKeys({
@UniqueKey(properties={"id"}),
@UniqueKey(properties={"name", "address"})
})
@MappedSuperclass
abstract public class Person {
}
UniqueKeys will be validated when persisting and changing entities before
committed to persistence, and user-friendly message will be displayed if
any unique constraint is violated.
Note that UniqueKeys annotation is for validation only and will not generate table unique constraints in DDL.
public MyEntityProperty<T extends PersistenceEntity> extends EntityProperty<T> {
@Override
public void validate(Object value) throws InvalidValueException, SystemException {
super.validate(value);
...
}
}
see Extensions.
@Override
public void validate() throws InvalidValueException, SystemException {
super.validate();
...
}
validate() method will be called before persisting or changing entities in
persistence.