无法运行java验证(JSR 303)

时间:2012-06-28 03:59:09

标签: java hibernate validation conflict bean-validation

最近,我决定使用JSR 303验证bean在我们的项目中,但是当我运行JUNI测试时,它出错了,日志如下:

java.lang.AbstractMethodError: org.hibernate.ejb.HibernatePersistence.getProviderUtil()Ljavax/persistence/spi/ProviderUtil;
    at javax.persistence.Persistence$1.isLoaded(Persistence.java:78)
    at org.hibernate.validator.engine.resolver.JPATraversableResolver.isReachable(JPATraversableResolver.java:61)
    at org.hibernate.validator.engine.resolver.DefaultTraversableResolver.isReachable(DefaultTraversableResolver.java:131)
    at org.hibernate.validator.engine.resolver.SingleThreadCachedTraversableResolver.isReachable(SingleThreadCachedTraversableResolver.java:46)
    at org.hibernate.validator.engine.ValidatorImpl.isValidationRequired(ValidatorImpl.java:1242)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:448)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:397)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:361)
    at org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:313)
    at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:139)
    at com.icil.service.validator.BeanValidator.validate(BeanValidator.java:74)
    at com.icil.sofs.booking.model.ValidateTest.test1(ValidateTest.java:48)
    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:616)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    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)

有人告诉我这是由于JAR文件冲突造成的。但我不知道哪个JAR存在冲突。任何人都可以告诉我哪些JAR文件存在冲突。对我来说任何帮助都很大,谢谢!

enter image description here

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

没有JAR列表,在这里很难帮到你。 你的项目中有maven吗?

如果是这样,你可以使用`mvn dependency:tree并查看jar树。

您可能正在使用不符合JPA 2.0规范的Hibernate规范ProviderUtil类是在JPA 2.0中引入的,因为doc说明: ProviderUtil

另一个问题是: 您是独立运行还是在某个容器内运行(应用程序/ Web服务器或其他东西) 例如,如果您正在运行JBoss,它在lib文件夹中有自己的jar,也可能与您的jar冲突。您是如何运行JUnit测试的?

答案 1 :(得分:0)

我遇到了类似的问题。实际上,HibernatePersistence在实体jar中没有ProviderUtil()方法。以下组合效果很好。

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
 </dependency>

 <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.5.Final</version>
</dependency>

答案 2 :(得分:0)

如果它只是验证问题,请考虑使用自己的TraversableResolver实现,而不是JPATraversableResolver或DefaultTraversableResolver。 Here's how to do it

JPATraversableResolver可能会导致对JPA的不受欢迎的依赖。 Here's a guy with a similar problem.

当然,如果您希望您的应用程序符合JPA2标准,那么这不是解决方案。

public class MyTraversableResolver implements TraversableResolver {

     public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class rootBeanType, Path pathToTraversableObject, ElementType elementType) {
        return true;
     }

     public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class rootBeanType, Path pathToTraversableObject, ElementType elementType) {
        return true;
     }
}