8 Replies Latest reply on Jan 16, 2008 3:09 AM by erikslagter

    EntityManager trouble

    erikslagter

      I'm in some trouble with testing my DAO in Eclipse. I'm using TestNG and Eclipse europe together with Seam 2.0.0GA.

      I have to got the following test funtion:

       @Test
       public void test(){
       dao = new PaymentDeviceDAO();
       String terminalProvider = "Malle Japie";
       String terminalId = "12345";
      
       device = dao.findById(terminalProvider, terminalId);
       assert device != null;
       }
      
       private EntityManagerFactory emf;
       @Out
       EntityManager em;
      
       protected String datasourceJndiName;
       public EntityManagerFactory getEntityManagerFactory(){
       assert emf != null;
       return emf;
       }
      
       @BeforeClass
       @Parameters("datasourceJndiName")
       public void setDatasourceJndiName(String datasourceJndiName) {
       this.datasourceJndiName = datasourceJndiName;
       init();
       }
      
       public void init(){
       emf = Persistence.createEntityManagerFactory(datasourceJndiName);
       em = emf.createEntityManager();
       assert emf != null;
       }
      
       @AfterClass
       public void destroy(){
       emf.close();
       assert emf.isOpen() == false;
       }
      


      What is tries to do is find a PaymentDevice using the two arguments. Nothing special...
      datasourceJndiName is defined in the testng.xml filed as "testDb".
      When I run this test I get the following Exception:
      javax.persistence.PersistenceException: No Persistence provider for EntityManager named testDb


      I tried to figure it out myself, by following the dvd example of seam and various forums.
      I can't find out anymore where it goes all wrong!?

      Can anybody help me?

      My configs are defined as following:
      persistence.xml
      
       <persistence-unit name="testDb">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/testDataSource</jta-data-source>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
       <property name="hibernate.cache.use_query_cache" value="true"/>
       <property name="hibernate.show_sql" value="false"/>
      
       <property name="jboss.entity.manager.factory.jndi.name" value="java:/testEntityManagerFactory" />
       </properties>
       </persistence-unit>
      


      components.xml
      <persistence:managed-persistence-context name="entityManager"
       auto-create="true"
       persistence-unit-jndi-name="java:/testEntityManagerFactory" />


      mps-ds.xml
      <local-tx-datasource>
       <jndi-name>testDataSource</jndi-name>
       <connection-url>jdbc:postgresql://localhost:5432/mps</connection-url>
       <driver-class>org.postgresql.Driver</driver-class>
       <user-name>postgres</user-name>
       <password>secretPassword:)</password>
      <!--
       <exception-sorter-class-name>
       org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
       </exception-sorter-class-name>
       <metadata>
       <type-mapping>mySQL</type-mapping>
       </metadata>
      -->
       </local-tx-datasource>


      hibernate.cfg.xml
      <hibernate-configuration>
       <session-factory>
      
       <property name="show_sql">false</property>
       <property name="connection.datasource">java:/testDataSource</property>
       <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
       <property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
       <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
       <property name="hbm2ddl.auto">create-drop</property>
      
       </session-factory>
      </hibernate-configuration>
      


        • 1. Re: EntityManager trouble
          pmuir

          How are you putting the datasource into JNDI?

          • 2. Re: EntityManager trouble
            erikslagter

            I don't know exactly what your asking me.
            Where would you normally do it?

            • 3. Re: EntityManager trouble
              erikslagter

              I just tried the DVDstore example and it fails on the same point in ProductionUnitTest.
              testNG stacktrace:

              javax.persistence.PersistenceException: No Persistence provider for EntityManager named dvdDatabase
               at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89)
               at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
               at com.jboss.dvd.seam.test.ProductUnitTest.em(ProductUnitTest.java:23)
               at com.jboss.dvd.seam.test.ProductUnitTest.testCreateDelete(ProductUnitTest.java:54)
               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.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
               at org.testng.internal.Invoker.invokeMethod(Invoker.java:478)
               at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:607)
               at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:874)
               at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
               at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
               at org.testng.TestRunner.runWorkers(TestRunner.java:689)
               at org.testng.TestRunner.privateRun(TestRunner.java:566)
               at org.testng.TestRunner.run(TestRunner.java:466)
               at org.testng.SuiteRunner.runTest(SuiteRunner.java:301)
               at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:296)
               at org.testng.SuiteRunner.privateRun(SuiteRunner.java:276)
               at org.testng.SuiteRunner.run(SuiteRunner.java:191)
               at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:808)
               at org.testng.TestNG.runSuitesLocally(TestNG.java:776)
               at org.testng.TestNG.run(TestNG.java:701)
               at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
               at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
              


              Is there something wrong with my install or with the unit test?

              • 4. Re: EntityManager trouble
                gduq

                I don't know if this helps. but I vaguely recall having to make some changes to get tests to run inside eclipse via the TestNG plugin (not sure if this is even what you are trying to do) one of the things I remember having to do was add the \resources folder to the build path.

                • 5. Re: EntityManager trouble
                  bvdberg

                  Well. the error

                  No Persistence provider for EntityManager named dvdDatabase

                  means that the EntityManagerFacrtory cannot find
                  the file 'META-INF/persistence.xml' which should be in your classpath.

                  Bas


                  • 6. Re: EntityManager trouble
                    bvdberg

                    also try this in components.xml

                    <persistence:managed-persistence-context name="entityManager"
                     auto-create="true"
                     entity-manager-factory="#{myEntityManagerFactory}"/>
                    
                     <persistence:entity-manager-factory name="myEntityManagerFactory"
                     persistence-unit-name="testDb"/>
                    
                    




                    • 7. Re: EntityManager trouble
                      pmuir

                      The unit test passes for me and in continuous integration. How are you running the test?

                      • 8. Re: EntityManager trouble
                        erikslagter

                        It's probably my Eclipse configuration. I stopped trying to use a real database (for testing) and now use a HSQLDB. Works fine for me...