5

使用 Hiberate 为持久层实现 Web 服务。效果很好,没有错误。但是,当我编写单元测试以访问 Dao 时,在获取 ServiceRegistry 的实例时遇到此错误:

ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();

完整的堆栈跟踪:

java.util.ServiceConfigurationError: org.hibernate.integrator.spi.Integrator: Provider org.jadira.usertype.dateandtime.joda.integrator.UserTypeJodaTimeHibernateIntegrator could not be instantiated: java.lang.NoClassDefFoundError: org/jadira/usertype/dateandtime/shared/spi/AbstractVersionableUserType
    at java.util.ServiceLoader.fail(ServiceLoader.java:224)
    at java.util.ServiceLoader.access$100(ServiceLoader.java:181)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:377)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:445)
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:236)
    at org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(IntegratorServiceImpl.java:53)
    at org.hibernate.service.internal.BootstrapServiceRegistryImpl.<init>(BootstrapServiceRegistryImpl.java:80)
    at org.hibernate.service.internal.BootstrapServiceRegistryImpl.<init>(BootstrapServiceRegistryImpl.java:57)
    at org.hibernate.service.ServiceRegistryBuilder.<init>(ServiceRegistryBuilder.java:76)
    at com.dg.dao.SessionConfig.getSession(SessionConfig.java:26)
    at com.dg.dao.CustomerHibernate.doGetSingleById(CustomerHibernate.java:210)
    at com.dg.dao.CustomerHibernate.getCustomerByID(CustomerHibernate.java:44)
    at com.dg.dao.test.CustomerDaoTest.testCustomerDao(CustomerDaoTest.java:27)
    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:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    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.NoClassDefFoundError: org/jadira/usertype/dateandtime/shared/spi/AbstractVersionableUserType

我已经验证类路径中不存在该类。为了纠正这个问题,我需要获取旧版本的 usertype.spi。

问题是,为什么它会在单元测试时尝试使用较旧的类文件,但在容器中运行服务时却工作得很好?有没有人见过这个问题?

4

1 回答 1

6

我发现这个问题是由以下依赖项之间的冲突引起的:

<dependency>
   <groupId>org.jadira.usertype</groupId>
   <artifactId>usertype.jodatime</artifactId>
   <version>2.0.1</version>
</dependency>

<dependency>
   <groupId>org.jadira.usertype</groupId>
   <artifactId>usertype.core</artifactId>
   <version>3.1.0.CR6</version>
</dependency>

问题的出现尤其是因为两个包都包含 PersistentDateTime 类(尽管还有许多其他重复的类)。PersistentDateTime 的两个实例都扩展了一个名为 AbstractVersionableUserType 的类。唯一的问题是,这里的基类都驻留在不同的包中。因此,hibernate 在创建 ServiceRegistry 时使用反射动态加载这些类型。它轰炸junit的原因是hibernate试图从usertype.jodatime包中加载PersistentDateTime类,而需要加载的实际类来自usertype.core包。它在容器中工作仅仅是因为使用了不同的类加载器。

无论如何,当删除 usertype.jodatime 依赖项时,一切正常。

于 2013-06-05T13:11:20.757 回答