javahibernatevalidationjavax

How are javax validator and hibernate validator related(if they are)? and what is the difference?


The question I am asking could be really simple(even foolish).

I was using validation in spring-hibernate MVC app. I am using constraints such as @NotNull, @Pattern which come from javax.validation packages. When I us constraints such as @Email it is imported from org.hibernate.validator.constraints. In the validator class I use ValidatorFactory and other classes or interfaces(such as Validation, Validator) all of which are from javax.validation. I got really confused with this.

Then I started going deeper into what exactly javax validator and hibernate validator packages are, and apparently they are implementations of JSR303.

To validate, I use

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();

To further confuse me, I read one more way of instantiating ValidatorFactory which goes like this -

ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory();

and it works fine!

Please help me understand both of these better.

Thanks for help.


Solution

  • As a whole lot of things in Java (security, JDBC, JPA, etc. etc.), there is a standard, call bean validation, that defines interfaces and standard classes (i.e. javax.validation classes), and you can choose between several implementations of these classes and interfaces (hibernate validator being one of them).

    The landing page of hibernate validator tells it:

    Hibernate Validator 5.x is the reference implementation Bean Validation 1.1!