我创建了一个 jUnit 测试来测试我的代码,当我尝试实例化我的对象时,出现此错误
java.lang.IllegalAccessError: tried to access method org.hibernate.cfg.Configuration.<init>(Lorg/hibernate/cfg/SettingsFactory;)V from class org.hibernate.ejb.Ejb3Configuration
at org.hibernate.ejb.Ejb3Configuration.<init>(Ejb3Configuration.java:134)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:124)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at gov.ic.isso.sentry.rest.data.CeReportDaoImpl.init(CeReportDaoImpl.java:41)
at gov.ic.isso.sentry.rest.data.DaoBase.<init>(DaoBase.java:20)
at gov.ic.isso.sentry.rest.data.CeReportDaoImpl.<init>(CeReportDaoImpl.java:33)
at gov.ic.isso.sentry.rest.AppTest.testApp(AppTest.java:41)
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 junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
我的 init 方法的代码是
protected void init() {
emf = Persistence.createEntityManagerFactory("ceDataSessionData");
em = emf.createEntityManager();
}
我的 jUnit 测试如下
public void testApp()
{
CeReportDao reportDao = new CeReportDaoImpl();
try {
reportDao.getMoneyTransferReportJson(100, "TESTUSER1", "NS", null);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
最佳答案
我意识到这是一个旧线程,但我只是想添加一些额外的信息。我的 Java 1.7
遇到了同样的问题项目使用 maven 3.3.3
.
我们的集成测试使用 Spring 3.1.4
工作正常,直到我添加了 hibernate-annotations (ver. 3.5.4)
对我们的一件工件的依赖。然后下游工件开始具有相同的 IllegalAccessError
上面提到过。
花了几个小时试图找出问题的原因后,我能够通过更改我之前添加到 hibernate-entitymanager
的 hibernate-annotations 依赖项来修复它。依赖性。这解决了下游集成测试问题。
我唯一提到这一点的是要注意,我在classpath
上从来没有不同版本的Hibernate jars。 。 Maven 将所有 hibernate 版本显示为 3.5.4,除了少数应该具有不同版本的 jar(例如 hibernate-common-annotations 3.2.0
)。因此,我相信上面关于拥有不同 Hibernate jar 的评论对于我来说不是问题。
关于hibernate - 测试 Hibernate JPA 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12976328/