188.8.131.52. Migrate to Hibernate Validator 4
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 ValidatorFactoryJBoss Enterprise Application Platform 6 binds a default ValidatorFactory to the JNDI context under the name
Understand life cycle triggered validationWhen used in combination with Hibernate Core 4, life-cycle based validation is automatically enabled by Hibernate Core.
- Validation occurs on entity
- 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.
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
none. Other valid values for this property are
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
Validatorinstance from the
- Inject Validator instances in your EJB, CDI bean or any other Java EE injectable resource.
- You can use the
ValidatorContextreturned by the
ValidatorFactory.usingContext()to customize your Validator instance. Using this API you can configure a custom
ConstraintValidatorFactory. These interfaces are specified in the Bean Validation specification and are new to Hibernate Validator 4.
Modify code to use the new Bean Validation constraintsThe 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:
- 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 constraintsIn Hibernate Validator 3, a custom constraint needed to implement the
org.hibernate.validator.Validatorinterface. In Hibernate Validator 4, you need to implement the
javax.validation.ConstraintValidatorinterface. This interface contains the same
isValid()methods as the previous interface, however, the method signature has changed. In addition,
DDLalteration is no longer supported in Hibernate Validator 4.