12.java - Course 3 - S13 - Bean Validation
12.java - Course 3 - S13 - Bean Validation
Constraint Definitions.
Constraint Descriptors.
Validation Groups.
Constraint Definitions
Constraint Descriptors
Validation Groups
Default group includes all constraints.
Validation Groups
Runtime exceptions
• ConstraintViolationException – generated by the framework
(JPA2) if validation failures occur and contains the set of
specific ConstraintViolation(s)
• ConstraintViolation – contains the failure details: constraint
descriptor, message, class, property and value
Integration Diagram.
Validator Factory.
Validation Modes.
Validation Groups.
Validation Exceptions.
Validation Overview
Validation is optional. The JPA 2.0 Spec does not require a Bean
Validation implementation.
Integration Diagram
javax.persistence d or
Provide
Default
EntityManagerFacto
javax.validation
Validation Configuration
ry Create ValidatorFacto
Attach
(mode and
to
validation groups) Create isTraverseablery Create
javax.validation
(entity)
TraversableResolver javax.validation
isLoaded(entit Validato
javax.persistence y) idate r
)
EntityManager Vaelntity
PrePersist (
Implicit or
Explicit
PreUpdate Validation Employee
Events (entity)
Manage @NotNull
PreRemove d String
name
Validator Factory
Java EE containers and Java SE applications can provide a
javax.persistence.validation.factory in the EMF properties Map.
Validation Modes
javax.persistence.ValidationMode
• Auto (default) – if a validation provider is available, then
validation should occur
• Callback – validation is required and a PersistenceException
must be thrown if a provider cannot be obtained
• None – no validation should be attempted and the lack of a
validation provider should not cause an exception
Validation Groups
Defines validation groups for entity life-cycle events
• javax.persistence.validation.group.pre-persist – Default
validation group called after all other PrePersist callbacks.
• javax.persistence.validation.group.pre-update – Default
validation group called after all other PreUpdate callbacks.
• javax.persistence.validation.group.pre-remove – Default
validation group is NOT called after all other PreRemove
callbacks.
Validation Exceptions
javax.persistence.PersistenceException – thrown if validation
mode is Callback and a provider could not be obtained
Integration Diagram.
LifecycleEventManager.
TraversableResolver.
Unit Testing.
Configuration Updates
New configuration properties
• javax.persistence.validation.factory – EMF property
• javax.persistence.validation.mode – PU or EMF property
• javax.persistence.validation.group.pre-persist – PU or EMF
property and entity annotation
Pluggable Validator
Removes dependency on JSR-303 APIs
Configuration Updates
Pluggable LifecycleEventManager
• Loaded during EMF creation in PersistenceProviderImpl by
loadValidator() just like loadAgent(), as old invocation location
in BrokerImpl was restricted to kernel classes
• Requires access to openjpa-persistence classes after all config
derivations are loaded
Integration Diagram
OpenJPAConfiguration
TraversableResolve
Validation r
ValidationMode
Configuration
Validator Impl (JSR-303 based)
Properties via
persistence.xml or Validation
Map on Factory
createEMF() Lifecycle Validation
Groups (Validating)
LifecycleEventManager
Validator
Broker Validating (JSR-303
LifecycleEventManage based)
(Validating) r Validation
Validat Groups
LifecycleEventMana
or Validation
ger
Factory
ValidatingLifecycle
Validator
EventManager created if:
- ValidationMode != NONE
- Validation API and factory
available
TALENTSPRINT | © Copyright 2015
Bean Validation with JPA
LifecycleEventManager
Extended to provide event based Validation
• ValidatingLifecycleEventManager
TraversableResolver
Provided to ValidationFactory upon Validator creation Simple
interface with single isTraversable method