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.

有帮助吗?

解决方案

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.

其他提示

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>

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>

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

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.

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.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top