6 Replies Latest reply on Mar 6, 2006 8:42 AM by neillane

    Entity Manager null

    neillane

      I have a servlet that is calling a Bean in my seam application.

      The servlet is working correctly and calls the Bean correctly, but when the bean needs to call the em.persist(object) method, the em is null.

      I used this bean in my app using beta1 and used an initial context lookup

       EnvelopeUpload uploader = null;
       try
       {
       uploader = Utils.getEnvelopeUploader();
       }
       catch (Exception e)
       {
       e.printStackTrace();
       }
      
      


      Utils returning

      try
       {
       log.info("Getting the initial Context");
       InitialContext ctx = new InitialContext();
       log.info(" got the context - " + ctx);
       EnvelopeUpload envelopeUpload = (EnvelopeUpload) ctx.lookup(EnvelopeUpload.class.getName());
       log.info("The envelope is - " + envelopeUpload);
       return envelopeUpload;
       }
       catch (Exception e)
       {
       e.printStackTrace();
       throw e;
       }
      


      If I try the Context lookup now in beta2 I get a "classNotFound" exception.

      I have also tried

      em = (EntityManager) Component.getInstance("em", true);


      And get
      IllegalStateException: No active application scope


      Please can someone let me know how to solve this issue

      Thanks

        • 1. Re: Entity Manager null

          Make sure that you have the seam servlet filter invoked before your filter and that you pass it conversationid as a parameter. See the documentation on the SeamServletFilter.

          the entry in the web.xml is this.

          <filter>
           <filter-name>Seam Servlet Filter</filter-name>
           <filter-class>org.jboss.seam.servlet.SeamServletFilter</filter-class>
          </filter>
          
          <filter-mapping>
           <filter-name>Seam Servlet Filter</filter-name>
           <url-pattern>/servlet/*</url-pattern>
          </filter-mapping>


          Hope this resolves your issue,

          James

          • 2. Re: Entity Manager null
            neillane

            Hi James

            I have changed the servlet mapping to be /servlet/.... and added the filter code to the web.xml file in my application as per your code snippet.

            I am able to call the doPost method in my servlet which in turn calls

            
             EnvelopeUploadBean uploader = null;
             try
             {
             uploader = new EnvelopeUploadBean();
             }
             catch (Exception e)
             {
             e.printStackTrace();
             }
            
             String result = uploader.uploadEnvelope(inputStream);
            
            


            This then runs everything in the *bean* correctly.

            However - I am still unable to get the EntityManager (it is still null when I am trying to persist the data to my database)

            I have searched the reference docs for info on the SeamServletFilter and how to use it with very little success.

            If possible, could you explain, or point me in right dicetion as to how to involk the filter and make it possible for me to use the EntityManager

            Thanks

            Neil

            • 3. Re: Entity Manager null
              andy.2003

              I'd got this porblems as well, while trying to inject the Entitymanager, my Problem was, that the bean accessing the EntityManager wasn't an ejb3 sessionbean. So this solved my problem:

              @Stateless
              public class foo{
               @PersistentContext EntityManager em
              ...
              }


              after making the Bean a Sessionbean the EntityManager was injected correctly

              • 4. Re: Entity Manager null
                neillane

                I have the following

                @Stateless
                @Name("envelopeUpload")
                public class EnvelopeUploadBean implements EnvelopeUpload, Serializable
                {
                
                 @PersistenceContext
                 private EntityManager em;
                
                .....
                
                


                I have even tried with the unit name of the context ( the DS) and that doesnt work either.

                The other Objects that I am injecting are working fine. I can get the Object the I am going to persist and set all the vars.



                • 5. Re: Entity Manager null
                  andy.2003

                  did you set up the datasource the right way? (persistence.xml etc.)

                  • 6. Re: Entity Manager null
                    neillane

                    I would think so.

                    All the other functions of retrieving data from the database and displaying it in datatables are working fine in my web app. It is just the bean that is called by the servlet, where the em is null and I cant seem to instantiate it.

                    The persistance.xml

                    <persistence>
                     <persistence-unit name="NerveCentre-DS">
                     <provider>org.hibernate.ejb.HibernatePersistence</provider>
                     <jta-data-source>java:/NerveCentreDS</jta-data-source>
                     <properties>
                     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
                     <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                     <property name="hibernate.transaction.flush_before_completion" value="true"/>
                     <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                     <property name="hibernate.show_sql" value="true"/>
                     </properties>
                     </persistence-unit>
                    </persistence>
                    


                    The DS

                    <?xml version="1.0" encoding="UTF-8"?>
                    
                    <datasources>
                     <local-tx-datasource>
                     <jndi-name>NerveCentreDS</jndi-name>
                     <connection-url>jdbc:mysql://127.0.0.1:3306/nerve-centre</connection-url>
                     <driver-class>com.mysql.jdbc.Driver</driver-class>
                     <user-name>root</user-name>
                     <password></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>
                    </datasources>