Hibernate - Error activating Bean Validation integ

2020-02-01 21:42发布

问题:

I am trying to set up Hibernate. But when i try to create my Session Factory, with this code:

Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

I get the error:

org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
        at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:156)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:303)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1760)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: java.lang.NoSuchMethodError: javax.validation.spi.ConfigurationState.getParameterNameProvider()Ljavax/validation/ParameterNameProvider;
        at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.<init>(ValidatorFactoryImpl.java:113)
        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)
        at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:445)
        at org.hibernate.cfg.beanvalidation.TypeSafeActivator.activate(TypeSafeActivator.java:96)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:150)
        ... 31 more

I have the hibernate-validator and the bean validator in my class-path (see screenshot)

What could be the issues here?

Edit I had two additional validation-api on the classpath, but excluded them. Could they be a problem somehow. Maybe the scope=provided?

<dependency>
    <groupId>com.google.gwt</groupId>
    <artifactId>gwt-user</artifactId>
    <version>${gwtVersion}</version>
    <scope>provided</scope>
    <exclusions>
        <exclusion>
            <artifactId>validation-api</artifactId>
            <groupId>javax.validation</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>com.google.web.bindery</groupId>
    <artifactId>requestfactory-server</artifactId>
    <version>2.5.1</version>
    <exclusions>
        <exclusion>
            <artifactId>validation-api</artifactId>
            <groupId>javax.validation</groupId>
        </exclusion>
    </exclusions>
</dependency>

Edit 2

Solved the problem: 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. Thanks a lot for the hints!

回答1:

NoSuchMethodError indicates that you have loaded wrong version of jar that has no method named like which you have used.
I highly recommend you to use hibernate 4.0 or little lower which turn out to be more stable to transplant.



回答2:

The problem arises from having similar packaged structure jar. In my case, I had javaee6 as provided, and javax.validation1.1.0.Final both have the package javax.validation.spi.

Since I used Maven, I placed javax.validation1.1.0 dependency on top of javaee6. So I can use 1.1.0 methods. Otherwise, if the javaee6 is on top I cannot access/ override validation1.1.0 methods.

It compiled fine, but while running in Tomee 1.7.5 WebProfile I faced NoSuchMethodError. So, added the javax.validation1.1.0 Final jar to lib folder of Tomee, but still somehow javaee6 jar's validation got triggered first.

So I renamed the file javax.validation1.1.0.Final to ijavax.validation1.1.0.Final, so that when alphabetically ordered it gets loaded before javaee6, then it worked!!

Check here for class loader ordering of jars in tomcat Order of loading jar files from lib directory