22

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.

Community
  • 1
  • 1
Ali
  • 261,656
  • 265
  • 575
  • 769
  • Click Upvote, I have the same problem. Could you show your final pom hibernate dependencies? – May12 Feb 04 '16 at 11:07

7 Answers7

20

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.

Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • That works! On the Hibernate Validator site, for 5.0.3.Final it says: `This transitively pulls in the dependency to the Bean Validation API (javax.validation:validation-api:1.1.0.Final).` I think that must be the issue, since changing the version seems to have fixed it. Do you know if forcing it to not pull the Bean Validation api is possible? – Ali Feb 09 '14 at 06:43
  • I was able to make this work with 5.0.3.Final. By adding a dependency to javax.validation:validation-api 1.1.0.Final , and making sure this dependency is placed before the hibernate-validatory dependency, it not works fine. Accepting your answer as you pointed me in the right direction. – Ali Feb 09 '14 at 07:12
  • O great, sometimes order of the dependencies enter in the game, in other situations is convenient to add to the dependencies to avoid download unwanted dependencies. – Koitoer Feb 09 '14 at 07:41
  • I did try with exclusion, but it didn't have any effect. Anyway, perhaps now you can also move on from 4.2.0 and upgrade to latest version ;) – Ali Feb 09 '14 at 07:58
  • 1
    Seems like you mixed BV 1.0 API with HV 5.x which requires BV 1.1. When adding HV 5 to your POM, Maven will definitely include the BV 1.1 API, so my guess is that you're pulling in the BV 1.0 API via another dependency. You could run `mvn dependency:tree` to see where it comes from. – Gunnar Feb 10 '14 at 10:54
  • In my case I added it as provided. The J2EE container should provide an implementation but it was needed during testing. – borjab Feb 03 '16 at 14:11
8

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>
Pijush
  • 417
  • 5
  • 3
  • Thank you for this answer, I had the same issue one project had version 1.0.0.GA-redhat-2 from JBoss EAP conflicting with verson 1.1.0.Final of another one. – Ben Jun 14 '17 at 09:14
3

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>
GtdDev
  • 748
  • 6
  • 14
2

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..

2

I thought it would be useful to explain what is going on here.

Hibernate is calling ConfigurationState.getParameterNameProvider:

ValidatorFactoryImpl.java:

public ValidatorFactoryImpl(ConfigurationState configurationState) {
   ...
   configurationState.getParameterNameProvider()
   ...
}

You can find the documentation of getParameterNameProvider:

getParameterNameProvider

ParameterNameProvider getParameterNameProvider()

Returns the parameter name provider for this configuration.

Returns:

parameter name provider instance or null if not defined

Since:

1.1

So what's the problem? The problem is that the method didn't always exist. It was added at some point in the future.

And the rule when creating interfaces is that they are set in concrete: you shall not change an interface ever. Instead the JavaX validator changed the ConfigurationState interface, and added a few new methods over the years.

The java validation code is passing the Hiberate an outdated ConfiguationState interface; one that doesn't implement the required interfaces.

You need to ensure that javax.validation.Validation.buildDefaultValidatorFactory is updated to to support version 1.1.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
1

Removing this jar javax.validation:validation-api:1.1.0.Final solved my problem.

Make sure you have only one validation jar. If we have two jars then they may conflict resulting in error.

RubioRic
  • 2,442
  • 4
  • 28
  • 35
0

Go to the dependecies project and delete, hibernate.validator, and reinstall that in the most recent version. It has solved the problem for me.

GtdDev
  • 748
  • 6
  • 14