3.2.2.12. Migrate to Hibernate Validator 4
Summary
Hibernate Validator 4.x is a completely new code base that implements JSR 303 - Bean Validation. The migration process from Validator 3.x to 4.x is fairly straightforward, but there are a few changes you must make when you migrate your application.
Procedure 3.18. You may need to perform one or more of the following tasks
Access the default ValidatorFactory
JBoss Enterprise Application Platform 6 binds a default ValidatorFactory to the JNDI context under the namejava:comp/ValidatorFactory
.Understand life cycle triggered validation
When used in combination with Hibernate Core 4, life-cycle based validation is automatically enabled by Hibernate Core.- Validation occurs on entity
INSERT
,UPDATE
, andDELETE
operations. - You can configure the groups to be validated by event type using the following properties:The values of these properties are the comma-separated, fully qualified class names of the groups to validate.
javax.persistence.validation.group.pre-persist
,javax.persistence.validation.group.pre-update
, andjavax.persistence.validation.group.pre-remove
.
Validation groups are a new feature of the Bean Validation specification. If you do not want to take advantage of this new feature, no changes are required when you migrate to Hibernate Validator 4. - You can disable life-cycle based validation by setting the
javax.persistence.validation.mode
property tonone
. Other valid values for this property areauto
(the default),callback
andddl
.
Configure your application to use manual validation
- If you want to manually control validation, you can create a Validator in either of the following ways:
- Create a
Validator
instance from theValidatorFactory
using thegetValidator()
method. - Inject Validator instances in your EJB, CDI bean or any other Java EE injectable resource.
- You can use the
ValidatorContext
returned by theValidatorFactory.usingContext()
to customize your Validator instance. Using this API you can configure a customMessageInterpolator
,TraverableResolver
andConstraintValidatorFactory
. These interfaces are specified in the Bean Validation specification and are new to Hibernate Validator 4.
Modify code to use the new Bean Validation constraints
The new Bean level validation constraints require code changes when you migrate to Hibernate Validator 4.- To upgrade to Hibernate Validator 4, you must use the constraints in the following packages:
javax.validation.constraints
org.hibernate.validator.constraints
- All constraints that existed in Hibernate Validator 3 are still available in Hibernate Validator 4. To use them, you need to import the specified class, and in some cases, change the name or type of the constraint parameter.
Use custom constraints
In Hibernate Validator 3, a custom constraint needed to implement theorg.hibernate.validator.Validator
interface. In Hibernate Validator 4, you need to implement thejavax.validation.ConstraintValidator
interface. This interface contains the sameinitialize()
andisValid()
methods as the previous interface, however, the method signature has changed. In addition,DDL
alteration is no longer supported in Hibernate Validator 4.