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

  1. Access the default ValidatorFactory

    JBoss Enterprise Application Platform 6 binds a default ValidatorFactory to the JNDI context under the name java:comp/ValidatorFactory.
  2. Understand life cycle triggered validation

    When used in combination with Hibernate Core 4, life-cycle based validation is automatically enabled by Hibernate Core.
    1. Validation occurs on entity INSERT, UPDATE, and DELETE operations.
    2. You can configure the groups to be validated by event type using the following properties:
      • javax.persistence.validation.group.pre-persist,
      • javax.persistence.validation.group.pre-update, and
      • javax.persistence.validation.group.pre-remove.
      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.
    3. You can disable life-cycle based validation by setting the javax.persistence.validation.mode property to none. Other valid values for this property are auto (the default), callback and ddl.
  3. Configure your application to use manual validation

    1. If you want to manually control validation, you can create a Validator in either of the following ways:
      • Create a Validator instance from the ValidatorFactory using the getValidator() method.
      • Inject Validator instances in your EJB, CDI bean or any other Java EE injectable resource.
    2. You can use the ValidatorContext returned by the ValidatorFactory.usingContext() to customize your Validator instance. Using this API you can configure a custom MessageInterpolator, TraverableResolver and ConstraintValidatorFactory. These interfaces are specified in the Bean Validation specification and are new to Hibernate Validator 4.
  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.
    1. To upgrade to Hibernate Validator 4, you must use the constraints in the following packages:
      • javax.validation.constraints
      • org.hibernate.validator.constraints
    2. 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.
  5. Use custom constraints

    In Hibernate Validator 3, a custom constraint needed to implement the org.hibernate.validator.Validator interface. In Hibernate Validator 4, you need to implement the javax.validation.ConstraintValidator interface. This interface contains the same initialize() and isValid() methods as the previous interface, however, the method signature has changed. In addition, DDL alteration is no longer supported in Hibernate Validator 4.