Hi everyone,

Currently, I need to set up and use the JPA entity manager not Ivy Entity Manager because our team want it to work with JUnit. (Ivy Entity Manager doesn't work with JUnit because of Ivy environment).

Is that possible to do so? I have tried edit the persistence.xml and try to create an entity manager but I've got the exception:

javax.persistence.PersistenceException: [PersistenceUnit: xrfl_unit] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at ch.soreco.standard.xrfl.service.TransactionManager.execute(TransactionManager.java:38)
    at ch.soreco.standard.xrfl.service.ValuationService.findAll(ValuationService.java:26)
    at ch.soreco.standard.xrfl.unittest.service.ValuationServiceTest.findAll(ValuationServiceTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    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: org.hibernate.service.jndi.JndiException: Error parsing JNDI name [XRFL]
    at org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:92)
    at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:63)
    at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:85)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:184)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:156)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:85)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:184)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:156)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1825)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1783)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
... 31 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getNameParser(Unknown Source)
    at org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:86)
... 45 more

If you have any idea or suggestion, please share with me.

Thank You.

asked 22.12.2014 at 03:47

Tran%20Cuong%20Truc's gravatar image

Tran Cuong Truc
61227
accept rate: 100%

edited 22.12.2014 at 09:49

Reguel%20Wermelinger's gravatar image

Reguel Werme... ♦♦
7.2k11345


The interface of IIvyEntityManager makes it possible to access the "real" JPA EntityManager by invoking createEntityManager(). So code that uses it could look like this:

private static List<String> getMatchingCountries(String country){
    IIvyEntityManager ivyEntityManager = Ivy.persistence().get("PERSISTENCE_UNIT_NAME");
    EntityManager em = ivyEntityManager.createEntityManager();
    try{
        em.getTransaction().begin();

        // DO FANCY PLAIN JPA STUFF
        Query countryQuery = em.createQuery(
            "SELECT DISTINCT a.country FROM Address a " +
            "WHERE a.country LIKE :userInput");
            countryQuery.setParameter("userInput", "%"+country+"%");
        List<String> result = (List<String>) countryQuery.getResultList();

        em.getTransaction().commit();
        return result;
    }
    finally
    {
        em.close();
    }
}
link

answered 22.12.2014 at 08:53

Reguel%20Wermelinger's gravatar image

Reguel Werme... ♦♦
7.2k11345
accept rate: 70%

Thanks for the idea but with this way, you still need the Ivy environment in order to get the EntityManager which will cause environment exception in JUnit.

(22.12.2014 at 09:14) Tran Cuong Truc Tran%20Cuong%20Truc's gravatar image

Thank you for your answer, but if you mock the Ivy class, you need to mock the .persistence().get("PERSISTENCE_UNIT_NAME") aswell. Unfortunately, I wasn't able to do that.

(18.02.2015 at 13:29) Lukas Schmid Lukas%20Schmid's gravatar image

hi @Reguel Werme... ♦♦ Is there any way to overcome this issue? i also want to create Java persistence manager for Junit inside java. Thanks

(02.02 at 04:25) trungdv trungdv's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×7
×6
×5

Asked: 22.12.2014 at 03:47

Seen: 2,763 times

Last updated: 02.02 at 04:25