4 Replies Latest reply on Apr 20, 2009 11:51 PM by Karl Beowulph

    Using EntityManager in a SFSB method with a4j:Support action?

    Karl Beowulph Newbie
      Hello.

      Is there a way to use an EntityManager within a Stateful session bean method which has been called by an <a4j:support /> tag?


      For example, assume I have two comboboxes and I want the values of them drive a query which will then populate a third.  The problem is that my EntityManager is coming up as null in my method when called from an <a4j:support action ="..." />. 

      I can't help but feel I'm overlooking a simple solution.  Do I need to resubmit the entire form?  Is there any other way to grab the EntityManager?  Can I get the items from the database in another way? Am I overlooking a simple solution?

      -------------------------------------------------------------------------
      ** JSF **
      <h:selectOneMenu value="#{personName}">
        <f:selectItem itemValue=""/>
        <f:selectItem itemValue="Steve"/>
        <f:selectItem itemValue="John"/>
        <a4j:support event="onchange" action="#{directory.findPeople()}" reRender ="peopleMatch"/>
      </h:selectOneMenu>

      <h:selectOneMenu value="#{cityName}">
        <f:selectItem itemValue=""/>
        <f:selectItem itemValue="Toronto"/>
        <f:selectItem itemValue="Calgary"/>
        <a4j:support event="onchange" action="#{directory.findPeople()}" reRender ="peopleMatch"/>
      </h:selectOneMenu>

      <h:selectOneMenu id="peopleMatch" value="#{directory.foundPerson}">
        <s:selectItems value = "#{directory.matchingPeople}" var="mPers" label="#{mPers.toString()}" />
      </h:selectOneMenu>

      ** SFSB **
      @Stateful
      @Name("directory")
      @Scope(ScopeType.SESSION)
      public class DirectoryBean implements Directory {

        @PersistenceContext(type = PersistenceContextType.EXTENDED)
        protected EntityManager entityManager;
        .
        .
        .

        public void findPeople() {
         if (personName == null || personName.length() == 0 || cityName == null
          || cityName.length() == 0)
            return;

          if (entityManager == null) 
            log.info("EntityManager is null?!"); //This is what's happening
          else {
            List <Person> matchingPeople = (List<Person>) entityManager.createQuery(
              "from Person p where name = :name and city = :city")
              .setParameter("name", personName)
              .setParameter("city", cityName)
              ;
          }  
      }
        • 1. Re: Using EntityManager in a SFSB method with a4j:Support action?
          Joshua Davis Expert

          Are you sure you're using the same SFSB every time?  When does the conversation begin and end?

          • 2. Re: Using EntityManager in a SFSB method with a4j:Support action?
            Karl Beowulph Newbie

            Since I have the SFSB in the SESSION scope, do I need to indicate a beginning and end?  I can refactor it a conversation instead if that helps.

            • 3. Re: Using EntityManager in a SFSB method with a4j:Support action?
              Karl Beowulph Newbie
              I've put the SFSB into the CONVERSATION scope, but I still get the same problem.

              ** SFSB **
              @Stateful
              @Name("directory")
              @Scope(ScopeType.CONVERSATION)
              public class DirectoryBean implements Directory {

                @PersistenceContext(type = PersistenceContextType.EXTENDED)
                protected EntityManager entityManager;

                @RequestParameter protected Long directoryId;
                .
                .
                .
                //This method gets called from pages.xml on entering the page.
                public void load() {
                  if (directoryId != null && (directory == null || directory.getId() != directoryId)){
                    start();
                    //Do loading stuff
                  }
                }

                @Begin
                public void start() {
                  log.info("Starting the conversation");
                }

                public void findPeople() {
                 if (personName == null || personName.length() == 0 || cityName == null
                  || cityName.length() == 0)
                    return;

                  if (entityManager == null)
                    log.info("EntityManager is null?!"); //This is what's happening
                  else {
                    List <Person> matchingPeople = (List<Person>) entityManager.createQuery(
                      "from Person p where name = :name and city = :city")
                      .setParameter("name", personName)
                      .setParameter("city", cityName)
                      ;
                  }

                @End
                public void saveDirectory(){
                  //Save stuff
                }

                @End
                public void cancel() {}
              }
              • 4. Re: Using EntityManager in a SFSB method with a4j:Support action?
                Karl Beowulph Newbie

                Turns out I'm just blind.  I was missing a .getResultList() at the end of my query. 


                After doing the conversation change it now usually works; seems a little random though.  (If it doesn't I restart JBoss, try again, and it usually does).