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

    Entity Manager null

    Neil Lane Newbie

      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;
       uploader = Utils.getEnvelopeUploader();
       catch (Exception e)

      Utils returning

       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)
       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


        • 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-name>Seam Servlet Filter</filter-name>
           <filter-name>Seam Servlet Filter</filter-name>

          Hope this resolves your issue,


          • 2. Re: Entity Manager null
            Neil Lane Newbie

            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;
             uploader = new EnvelopeUploadBean();
             catch (Exception e)
             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



            • 3. Re: Entity Manager null
              Andreas Berger Newbie

              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:

              public class foo{
               @PersistentContext EntityManager em

              after making the Bean a Sessionbean the EntityManager was injected correctly

              • 4. Re: Entity Manager null
                Neil Lane Newbie

                I have the following

                public class EnvelopeUploadBean implements EnvelopeUpload, Serializable
                 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
                  Andreas Berger Newbie

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

                  • 6. Re: Entity Manager null
                    Neil Lane Newbie

                    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-unit name="NerveCentre-DS">
                     <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"/>

                    The DS

                    <?xml version="1.0" encoding="UTF-8"?>