Cmobilecom JPA 2.0.1 Developer Guide

22. Validation

Bean validation is defined in JSR 380. Managed classes(beans) can be automatically validated in pre-persist, pre-update and pre-remove lifecycle events if bean validation is enabled.

If validation mode is AUTO and a validation provider is found in classpath, or if validation mode is CALLBACK (a validation provider is required in classpath), then bean validation is enabled. For example,


<presistence ... >
    <persistence-unit name="pu_name">
Bean validation annotations:
public class Person {
    @Column(length = 10)
    private String id;

    @Column(length = 20, nullable = false)
    private String name;

    @Past // bean validation
    protected Date birthday;

public class Employee extends Person {
    @Null(groups = ValidationGroup1.class)  // bean validation
    private Employee manager;
    private String jobTitle;

    @Valid // bean validation: cascade
    @Size(max = 1, groups = ValidationGroup1.class) // bean validation
    private List<PhoneNumber> phoneNumbers;

    @Valid // bean validation: cascade
    private Address address;

public class PhoneNumber {
    @NotNull  // bean validation
    @Size(max = 3) // bean validation
    private String countryCode;

    @Size(min = 1, max = 3)  // bean validation
    private String areaCode;

    @Size(min = 7, max = 7) // bean validation
    private String localNumber;
public class Address {
    @Column(length = 50)
    private String street;
    @Column(length = 30)
    private String city;
    @Column(length = 20)
    private String state;
    @Valid // bean validation: cascade
    @NotNull // bean validation
    private ZipCode zipCode;
public class ZipCode {
    @Column(length = 5)
    @Pattern(regexp="\\d{5}") // bean validation
    private String majorCode;
    @Column(length = 4)
    @Pattern(regexp="\\d{4}") // bean validation
    private String minorCode;

Validation Groups

By default the validation DEFAULT group is validated for pre-persist and pre-update lifecycle events, but it will not be validated for pre-remove lifecycle event. To override validation groups for those lifecycle events, set the following properties in persistence.xml or when creating a EntityManagerFactory.
They are defined in com.cmobilecom.jpa.Constants. Their values are a list of full qualified group class names separated by comma. For example,
    import com.cmobilecom.jpa.Constants;
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(Constants.VALIDATION_GROUP_PRE_PERSIST, "com.cmobilecom.jpa.example.ValidationGroup1,com.cmobilecom.jpa.example.ValidationGroup2");  
    properties.put(Constants.VALIDATION_GROUP_PRE_UPDATE, "com.cmobilecom.jpa.example.ValidationGroup3");
    properties.put(Constants.VALIDATION_GROUP_PRE_REMOVE, "com.cmobilecom.jpa.example.ValidationGroup4");
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu_name", properties);
    EntityManager em = emf.createEntityManager();
ConstraintViolationException will be thrown if there are any validation constraints violated when the method persist(entity), merge(entity) or remove(entity) of EntityManager is called.
Query Properties/HintsMultitenantFrames / No Frames