EclipseLink/Development/JPA 2.0/validation api integration
JPA 2.0: Validation API Integration
JPA 2.0 spec mandates that a persistence provider needs to validate managed classes (entities, mapped superclasses, and embeddable classes) using Bean Validation API(JSR-303) on lifecycle events. Please see JPA spec section 3.6 for more details.
- EclipseLink should not require either Bean Validation API or a Bean Validation Provider at run time if Bean Validation is not enabled.
- There should be no performance overhead at runtime if Bean Validation is disabled
- There should be no dependency from foundation to jpa module
Bootstrapping of ValidatorFactory and other Validation metadata
Validation Mode: Specified by user as value of property "javax.persistence.validation.mode" or as "validation-mode" element in persistence.xml.
Validation Groups: Specified by user as value of property "javax.persistence.validation.group.pre-*". It is specified as comma separated fully qualified class names of the groups.
ValidatorFactory: An instance of the factory would be used to obtain Validator used to perform validation. ValidatorFactory is obtained in one of following ways as specified by JPA spec:
- Passed as value of property "javax.persistence.validation.factory" while creating EMF using Persistence.createEMF() or from a JavaEE container
- While running in JavaSE mode, created by EclipseLink using Validation.buildDefaultValidatorFactory()
Enabling Automatic Bean Validation on lifecycle events
- Introduce a DescriptorEventListener listener called BeanValidationListener.
- BeanValidationListener would be initialized with ValidatorFactory, Validation Mode, Validation Group and a pointer to the MetaDataProject
- Install this listener at deployment time if Bean Validation is enabled
- BeanValidationListener performs Bean Validation on life cycle events prePersit, preUpdate and preRemove
Disabling cascading of validation
Spec requires that while executing automatic validation, Validation cascade (@Valid) must not occur for entity associations (single- or multi-valued).
- When obtaining a Validator instance to perform validation, supply a TraversableResolver that can identify entity associations.
- Need to expand on this further
- Bean Validation Meta Data constraints like @Size can be used at DDL generation time.