Error testing Hibernate JPA

I create a jUnit test to test my code, when I try to initialize my object, I get this error

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) 

My code for my init method is

 protected void init() { emf = Persistence.createEntityManagerFactory("ceDataSessionData"); em = emf.createEntityManager(); } 

My jUnit test matches

  public void testApp() { CeReportDao reportDao = new CeReportDaoImpl(); try { reportDao.getMoneyTransferReportJson(100, "TESTUSER1", "NS", null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 
+4
source share
2 answers

I understand that this is an old thread, but I just thought that I would add more information. I ran into this problem with our Java 1.7 project using maven 3.3.3 .

Our integration tests using Spring 3.1.4 worked fine until I added the hibernate-annotations (ver. 3.5.4) dependency to one of our artifacts. Then the artifact downstream begins to have the same IllegalAccessError mentioned above.

After spending hours trying to figure out the cause of the problem, I was able to fix it by changing the hibernate-annotations dependency, which I previously added to the hibernate-entitymanager . This fixed downstream integration testing issues.

My only mention of this was to note that I have never used different versions of Hibernate jars on a classpath . Maven showed all versions of sleep mode as 3.5.4, with the exception of several jars that should have different versions (for example, hibernate-common-annotations 3.2.0 ). So, I think the comment above is that different banks of sleep mode are not a problem in my case.

+1
source

@Vikdor answered the question:

It seems that incompatible banks in sleep mode on the way to classes. Could you list the banks associated with hibernate along with their versions?

That was the problem. I had a dependency project that was pulling an old version of class permissions for hibernation, and this project was pulling the latter and therefore.

0
source

Source: https://habr.com/ru/post/1440798/