hibernate - 测试 Hibernate JPA 时出错

标签 hibernate jpa

我创建了一个 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/

相关文章:

java - 巩固 Thymeleaf 中的一对多关系

java - Spring 数据存储库 : Detached entity passed to persist

java - 如何使用 JPA API 构造此连接查询?

java - 如何使用 Spring MVC 和 hibernate 连接/绑定(bind)两个表

java - orderBy 在集合字段上的用途?

当并非所有子类型都有自己的表时,Hibernate、JPA、连接表继承导致循环依赖错误

java - 如何构建可嵌入类型的 ElementCollection?

java - 在 Spring Boot 中按 @onetomany 集合大小排序的最佳方法?

java - hibernate 还是 'Do-It-Yourself"?

java - 使用带有 Hibernate 的 Spring 数据 jpa 的具有相同标识符的不同对象