Validation

Entities need to be validated before they are created or merged in persistence. Property validation is property-level validation such as length, pattern, etc. However, entity validation is entity-level validation such as unique keys.

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.

Required

Whether a property is required is based on JPA annotation. For example,

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

Length

Length validation is for properties of String type. Property length can be annotated by JPA annotations. For example,

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

Value Range

Value range validation is for numeric values. For example,

	@Property(name="size", minValue=100, maxValue=500)
The property value must be in the range from 100 to 500, inclusive.

Regular Expressions

Pattern validation is for properties of String type. For example,

	@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");

Unique Keys

UniqueKeys annotation describe one or more unique keys of an entity type. Different from JPA UniqueConstraint for entity tables, UniqueKeys can annotate an entity type or its superclass, and UniqueKeys annotation in a subclass will override that of its superclass. For example,

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

Extends EntityProperty

If a subclass of EntityProperty is defined, it can validate property value by overriding validate(Object) method.

public MyEntityProperty<T extends PersistenceEntity> extends EntityProperty<T> {
	@Override
	public void validate(Object value) throws InvalidValueException, SystemException {
		super.validate(value);
		...
	}
}
see Extensions.

Entity Validation

Override validate() method of EntityBackingBean or EntityListBackingBean to provide more validations.

	@Override
	public void validate() throws InvalidValueException, SystemException {
		super.validate();
		...
	}
validate() method will be called before creating and changing entities in persistence.

Client Bean Validation

When a property value is changed, the following constraints will be validated on client: