编程语言
首页 > 编程语言> > java – 由于Bean Validation API而无法启动Hibernate Validator

java – 由于Bean Validation API而无法启动Hibernate Validator

作者:互联网

我正在尝试在我的项目中使用Hibernate Validator,但它无法正常工作.在以下行:

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

我得到以下异常:

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)

我发现this question看起来和我的问题非常相似.他将他的解决方案描述为

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.

我认为我的问题是一样的.在http://hibernate.org/validator/documentation/getting-started/它说:

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

这必定会导致此问题,因为恢复到旧版本(4.3.1.Final)可以解决问题.有没有办法强制Hibernate不引入Bean Validation API?

编辑:我试图排除javax验证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>

但它似乎没有任何影响.

解决方法:

尝试将此依赖项添加到您的pom.xml

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

如果不考虑使用hibernate-validator4.2.0.Final我在我的配置中有一个它工作正常.

标签:hibernate-validator,java,maven,hibernate
来源: https://codeday.me/bug/20190928/1829592.html