11 Replies Latest reply on Apr 22, 2011 10:24 AM by bonissauro

    Problems with EntityManager in SeamTest

    boopet

      Hello,


      we are using JBoss Seam 2.1.0 with a JBoss 4.2.2 Server.


      Currently are trying to test the persistence as described in the JBoss Seam book in our Testcase:


      EntityManagerFactory emf = Persistence.createEntityManagerFactory("project");
                this.em = emf.createEntityManager();
      
      



      the persistence.xml file (dev) has following contents:


      <persistence-unit name="project">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/DefaultDS</jta-data-source>
            <properties>
               <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.cache.use_second_level_cache" value="false"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/qads21EntityManagerFactory"/>
            </properties>
         </persistence-unit>
      
      



      Now the problem: When running the Testsuite within TestNG, the following error occurs:


      javax.persistence.PersistenceException: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
           at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:663)
           at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
           at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
           at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
      



      Does anyone have a clue? Is there a document describing differences from the current Seam version to the one in the Seam book?


      Thanks in advance for your help!


      Regards,
      Norbert

        • 1. Re: Problems with EntityManager in SeamTest
          cpopetz

          I coincidentally just ran into this today as well.  You need:


                   <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
          



          in your persistence.xml.  I've never had that in my persistence.xml before, and it used to work in test land without it, and it definitely works in-container without it, so I don't know what changed.  I'm guessing an upgrade to seam at some point pickup up a new version of hibernate in hibernate-all, and maybe hibernate started requiring it.  In any case, a quick check of the hibernate  docs says that we should have this in our persistence.xml to use hibernate with JTA.


          • 2. Re: Problems with EntityManager in SeamTest
            boopet

            thanks for the hint! that really helped.
            But now it seems its not possible to persist data.
            When persisting and trying to fetch with the entitymanger, nothing is returned as result.


            the old way using


            em.getTransaction().begin();
            ..
            em.getTransaction().commit();
            
            



            does no longer work now, because it says:


            java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
                 at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:317)
            



            thanks for any advice.


            regards,
            norbert

            • 3. Re: Problems with EntityManager in SeamTest
              pmuir

              Use an integration test, rather than trying to test persistence in a unit test.

              • 4. Re: Problems with EntityManager in SeamTest
                boopet

                thanks pete, this seems to be the right way for doing that.
                i have restructured my tests and now want to access the seam components that way:


                     @Test
                     public void testCreateUser() throws Exception {
                          
                          new ComponentTest() {
                               
                               protected void testComponents() throws Exception {
                                    
                                    User currentUser = (User) Component.getInstance("QAdsUser");
                                    UserService service = (UserService) Component.getInstance("userService");
                
                



                which brings up a NullPointerException when trying to access the components. I dont want to use the EL way to test basic functionality of the services.


                is there a way to get the components directly from the seam context inside a integration test?


                thanks
                norbert

                • 5. Re: Problems with EntityManager in SeamTest
                  pmuir

                  Use a FacesRequest.

                  • 6. Re: Problems with EntityManager in SeamTest
                    jwi.jwi.informationsdesign.de

                    ...have stumbled upon the same problem.


                    But no data get persistet, although using a FacesRequest.




                    excerpt from SQl-logging (no inserts/updates !?)


                    Hibernate: select SEQ_GESELLSCHAFT.nextval from DUAL
                    Hibernate: select entityfiel0_.ID as ID4_, entityfiel0_.cat as cat4_, entityfiel0_.dimType as dimType4_, entityfiel0_.fieldname as fieldname4_, entityfiel0_.fieldtype as fieldtype4_, entityfiel0_.relatedDimType as relatedD6_4_, entityfiel0_.standardSelection as standard7_4_, entityfiel0_.subsetType as subsetType4_, entityfiel0_.systemField as systemFi9_4_, entityfiel0_.textLength as textLength4_, entityfiel0_.valueList as valueList4_ from FIELDMETA entityfiel0_ where entityfiel0_.dimType='GESELLSCHAFT'
                    Hibernate: select fieldvalue0_.fieldMeta_ID as fieldMeta3_1_, fieldvalue0_.ID as ID1_, fieldvalue0_.ID as ID2_0_, fieldvalue0_.fieldMeta_ID as fieldMeta3_2_0_, fieldvalue0_.value as value2_0_ from FIELDVALUEITEM fieldvalue0_ where fieldvalue0_.fieldMeta_ID in (?, ?)
                    Hibernate: select SEQ_JOURNALDATA.nextval from dual
                    Hibernate: select SEQ_JOURNALDATA.nextval from dual
                    


                    • 7. Re: Problems with EntityManager in SeamTest
                      jwi.jwi.informationsdesign.de

                      as you can see, the sequences (it's an Oracle10) for inserting 2 new records are used (native SQL query), but no line insert ... is logged.


                      ?

                      • 8. Re: Problems with EntityManager in SeamTest
                        ajordens.adam.jordens.org

                        I use Maven and have my EJBs and actions separated into different artifacts.


                        I'm able to override the persistence.xml for the ejb artifact and do persistence testing without problem independent of the SeamTest.


                            <persistence-unit name="testDB">
                                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                                <class>...</class>    
                                <properties>
                                    <property name="hibernate.dialect"
                                              value="org.hibernate.dialect.HSQLDialect"/>
                                    <property name="hibernate.connection.driver_class"
                                              value="org.hsqldb.jdbcDriver"/>
                                    <property name="hibernate.connection.username" value="sa"/>
                                    <property name="hibernate.connection.password" value=""/>
                                    <property name="hibernate.connection.url" value="jdbc:hsqldb:."/>
                                    <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                                    <property name="hibernate.show_sql" value="true"/>
                                </properties>
                            </persistence-unit>



                        Tests look similar to the following (minus some abstractions to create the entity manager in a @BeforeSuite and cleanup in an @AfterSuite).


                            EntityManagerFactory factory = Persistence.createEntityManagerFactory("testDB");
                            EntityManager em = factory.createEntityManager();
                        
                            em.getTransaction().begin();
                            EntityA entity = new EntityA();
                            entity.setLabel("Foo");
                            em.getTransaction.commit();
                        
                            Assert.assertNotNull(entity.getId());
                        
                            em.close()
                            factory.close()


                        • 9. Re: Problems with EntityManager in SeamTest
                          btonez

                          I had a similar problem and solved it by changing the transaction type from default JTA (evil) to RESOURCE_LOCAL (Seam-friendly):


                          <persistence-unit
                                    name="project"
                                    transaction-type="RESOURCE_LOCAL">

                          • 10. Re: Problems with EntityManager in SeamTest
                            ellis2323.lmallet.oxalya.com

                            No Need to change the persistence.xml or whatelse.
                            By default, it seems that in a SeamTest, there is no Transaction created. Because of JTA we can't use getTransaction. So i created one with UserTransaction class.


                            @Name("testView")
                            public class TestView extends SeamTest {
                               @Test
                                public void testDeleteView() throws Exception {
                                    new ComponentTest() {
                                        protected void testComponents() throws Exception {
                                            UserTransaction tx = Transaction.instance();
                                            tx.begin();
                                            
                                            RPBElement rPBElement = (RPBElement)Component.getInstance("rPBElement");
                                            
                                            List<RPMCriterium> criteria = new ArrayList<RPMCriterium>();
                                            criteria.add(new RPMCriterium("Type", "testDeleteView", Comparator.EQ));                
                                            RPMView viewA = rPBElement.createViewExt("View testDelete", criteria);
                            
                                            tx.commit();         
                                            
                                            tx = Transaction.instance();
                                            tx.begin();
                                            
                                            rPBElement.deleteViewExt(viewA.getId());
                                            
                                            tx.commit();    
                                        }
                                    }.run();
                                }
                            }
                            



                            You could use your entityManager in this code with :


                            EntityManager entityManager = Component.getInstance("entityManager");
                            



                            • 11. Re: Problems with EntityManager in SeamTest
                              bonissauro

                              Great, Clint !!!


                              That was just what I needed !!!


                              10x a lot