<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How to use JPA entity manager instead of Ivy Entity Manager?</title><link>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager</link><description>&lt;p&gt;Hi everyone,&lt;/p&gt;
&lt;p&gt;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).&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;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.&amp;lt;init&amp;gt;(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
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you have any idea or suggestion, please share with me.&lt;/p&gt;
&lt;p&gt;Thank You.&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager" rel="self"></atom:link><language>en</language><lastBuildDate>Fri, 02 Feb 2018 04:25:36 -0500</lastBuildDate><item><title>Comment by trungdv on Reguel Wermelinger's answer</title><link>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager#3095</link><description>&lt;p&gt;hi &lt;a href="https://answers.axonivy.com/users/17/reguel-wermelinger"&gt;&lt;a href="https://answers.axonivy.com/users/17/reguel-wermelinger"&gt;@Reguel&lt;/a&gt;&lt;/a&gt; Werme... ♦♦
Is there any way to overcome this issue? i also want to create Java persistence manager for Junit inside java. 
Thanks&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">trungdv</dc:creator><pubDate>Fri, 02 Feb 2018 04:25:36 -0500</pubDate><guid>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager#3095</guid></item><item><title>Comment by Lukas Schmid on Reguel Wermelinger's answer</title><link>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager#1170</link><description>&lt;p&gt;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.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lukas Schmid</dc:creator><pubDate>Wed, 18 Feb 2015 13:29:51 -0500</pubDate><guid>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager#1170</guid></item><item><title>Comment by Reguel Wermelinger on Reguel Wermelinger's answer</title><link>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager#1105</link><description>&lt;p&gt;You need to mock out the Ivy class as described here: &lt;a href="http://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception"&gt;http://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Mon, 22 Dec 2014 09:28:22 -0500</pubDate><guid>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager#1105</guid></item><item><title>Comment by Tran Cuong Truc on Reguel Wermelinger's answer</title><link>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager#1104</link><description>&lt;p&gt;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.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tran Cuong Truc</dc:creator><pubDate>Mon, 22 Dec 2014 09:14:44 -0500</pubDate><guid>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager#1104</guid></item><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager/1103</link><description>&lt;p&gt;The interface of IIvyEntityManager makes it possible to access the "real" JPA EntityManager by invoking &lt;a href="http://developer.axonivy.com/doc/latest/publicApi/ch/ivyteam/ivy/process/data/persistence/IIvyEntityManager.html#createEntityManager()"&gt;createEntityManager()&lt;/a&gt;. So code that uses it could look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;private static List&amp;lt;String&amp;gt; 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&amp;lt;String&amp;gt; result = (List&amp;lt;String&amp;gt;) countryQuery.getResultList();

        em.getTransaction().commit();
        return result;
    }
    finally
    {
        em.close();
    }
}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Mon, 22 Dec 2014 08:53:05 -0500</pubDate><guid>https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager/1103</guid></item></channel></rss>