Hibernate - 激活Bean Validation集成时出错

时间:2013-06-23 23:38:13

标签: java hibernate validation bean-validation hibernate-validator

我正在尝试设置Hibernate。但是当我尝试使用以下代码创建会话工厂时:

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

我收到错误:

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

我的class-path中有hibernate-validator和bean验证器(见截图)enter image description here

这里可能出现什么问题?

编辑我在类路径上有两个额外的validation-api,但是将它们排除在外。他们可能会以某种方式成为问题。也许范围=提供?

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

修改2

解决了问题:我在类路径中有另一个bean验证器jar。但不是来自maven,所以我没有意识到这一点。删除它解决了问题。非常感谢提示!

2 个答案:

答案 0 :(得分:4)

NoSuchMethodError表示您加载了错误版本的jar,其中没有任何名称与您使用过的方法相同 我强烈建议您使用hibernate 4.0或更低版本,这样移植更稳定。

答案 1 :(得分:0)

问题来自具有类似的包装结构罐。 就我而言,我提供了javaee6,并且javax.validation1.1.0.Final都具有软件包javax.validation.spi

由于我使用了Maven,因此我将javax.validation1.1.0依赖项放在javaee6之上。所以我可以使用1.1.0方法。否则,如果javaee6在顶部,我将无法访问/覆盖validation1.1.0方法。

它编译良好,但是在Tomee 1.7.5 WebProfile中运行时,我遇到了NoSuchMethodError。因此,将Javax.validation1.1.0最终jar添加到Tomee的lib文件夹中,但仍然以某种方式首先触发了javaee6 jar的验证。

因此,我将文件javax.validation1.1.0.Final重命名为ijavax.validation1.1.0.Final,以便按字母顺序将其加载到javaee6之前,然后它可以正常工作!

在此处检查tomcat Order of loading jar files from lib directory中jar的类加载器顺序