Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble starting Hibernate Validator due to Bean Validation API

I'm trying to use Hibernate Validator in my project, but it isn't working. On the following line:

SessionFactory sessions = config.buildSessionFactory(builder.build());

I get the following exception:

org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
    at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:154)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:311)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
    at net.myProject.server.util.HibernateUtil.<clinit>(HibernateUtil.java:32)
    ... 36 more
Caused by: java.lang.NoSuchMethodError: javax.validation.spi.ConfigurationState.getParameterNameProvider()Ljavax/validation/ParameterNameProvider;
    at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.<init>(ValidatorFactoryImpl.java:119)
    at org.hibernate.validator.HibernateValidator.buildValidatorFactory(HibernateValidator.java:45)
    at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:217)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)

I found this question which seems quite similar to my problem. He describes his solution as

I had yet another bean validator jar in the class path. But not from maven, so i didn't realize it. Removing that solved the problem.

I think my problem is the same. On http://hibernate.org/validator/documentation/getting-started/ it says:

This transitively pulls in the dependency to the Bean Validation API (javax.validation:validation-api:1.1.0.Final)

That must be causing this issue, since reverting to an older version (4.3.1.Final) fixes the issue. Is there a way to force Hibernate to not pull in the Bean Validation API?

Edit: I've tried to exclude the javax-validation api:

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>5.0.3.Final</version>
      <exclusions>
          <exclusion>
              <groupId>javax.validation</groupId>
              <artifactId>validation-api</artifactId>
          </exclusion>
      </exclusions>
  </dependency>

But it didn't seem to have any effect.

like image 551
Ali Avatar asked Feb 09 '14 06:02

Ali


People also ask

What is Bean Validation API?

Bean validation API offers some very useful annotations that can be applied on a bean property for the purpose of maintaining data integrity. 1. Maven Given below are the required hibernate validator maven dependencies.

What kind of constraints does Hibernate Validator provide?

In addition to the constraints defined by the Bean Validation API, Hibernate Validator provides several useful custom constraints which are listed below. Checks that the annotated character sequence passes the Luhn checksum test. Note, this validation aims to check for user mistakes, not credit card validity!

How to use Hibernate Validator within a Maven project?

In order to use Hibernate Validator within a Maven project, simply add the following dependency to your pom.xml: This transitively pulls in the dependency to the Bean Validation API (javax.validation:validation-api:2.0.1.Final).

What are the JSRs required for Hibernate Validator?

This transitively pulls in the dependency to the Bean Validation API ( javax.validation:validation-api: ). Hibernate Validator also requires an implementation of the Unified Expression Language ( JSR 341) for evaluating dynamic expressions in constraint violation messages.


4 Answers

Try adding this dependency to your pom.xml

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
</dependency>

If not consider using hibernate-validator4.2.0.Final I have that one in my config and it is working fine.

like image 77
Koitoer Avatar answered Oct 17 '22 22:10

Koitoer


For me, the 1.1.0.Final version javax.validation.validation-api had worked. Because, the javax.validation.spi.ConfigurationState interface of 1.1.0.Final has getParameterNameProvider method, which was absent in 1.0.0.GA.

I added the below dependency in pom.xml

<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
           <scope>test</scope>
</dependency>
like image 20
Pijush Avatar answered Oct 17 '22 22:10

Pijush


I had the problem again. Thats how I've fixed that:

1-Exclude spring.validator from the 'web' dependency:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate.validator</groupId>
                <artifactId>hibernate-validator</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

2-After insert the dependecy with a previous version:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.3.Final</version>
    </dependency>
like image 3
GtdDev Avatar answered Oct 17 '22 20:10

GtdDev


in my case i just deleted the hibernate-validator and it worked .(i also had a combo of both validation api and hibernate-validator and tried everything) or you can go to your maven repository-->org and then delete the hibernate folder and rebuild your project again.. hope it helps..

like image 2
siddharth chaudhary Avatar answered Oct 17 '22 20:10

siddharth chaudhary