mavenjakarta-eebean-validationnhibernate-validator

Bean Validation API needed, when using Hibernate Validator Engine?


In my Java EE 7 OpenShift project I'm using javaee-api version 7.0. For my model I added the depency validation-api version 1.1.0, but that wasn't enough. Then I tried with

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>5.2.4.Final</version>
  <scope>provided</scope>
</dependency>

and this works. Is the validation-api implicit in this depency?


Solution

  • Bean Validation and Hibernate Validator

    The Hibernate Validator is the reference implementation of the Bean Validation 1.1 specification.

    Check the Maven Repository, for example, and you'll realize the hibernate-validator artifact does include the validation-api artifact as a dependency.

    Bean Validation and Java EE 7 API

    The Bean Validation is defined in the javax.validation package and sub-packages.

    The Java EE 7 is an umbrella specification and, among other specifications, it includes the Bean Validation 1.1 specification.

    It means the javaee-api artifact includes the javax.validation package and sub-packages. However, no implementation for the Bean Validation is provided as dependency of that artifact.

    In the other hand, some containers, such as WildFly and GlassFish, provide the Hibernate Validator dependency (or any other Bean Validation implementation) to be compliant with the Java EE 7 specification. In this situation, if you want to use something specific from the Hibernate Validator (that is, something from the org.hibernate.validator package or sub-packages), you need to add the hibernate-validator dependency with the provided scope.

    Otherwise, if you won't use anything specific from the Hibernate Validator, the javaee-api dependency should be just fine.