2 Replies Latest reply on Jun 17, 2008 2:39 PM by sandman202

    Action Bean using Component.getInstance()

    sandman202

      I have a question I hope someone could answer. I created an action bean using seam. This bean is importing city-state-zip records from a csv file and saving them into a MySQL database. As the loop reads each line it calls the following method:



              public void processInCSZData(HashMap<String, String> inStringMap)
                              throws Exception {
      
                      //InCityStateZip newInCityStateZip = (InCityStateZip) Contexts.lookupInStatefulContexts("newInCityStateZip");
                      //InCityStateZip newInCityStateZip = (InCityStateZip) Component.getInstance("newInCityStateZip");
                      InCityStateZip newInCityStateZip = new InCityStateZip();
                      
                      newInCityStateZip.setInFiles(newInFiles);
      
                      newInCityStateZip.setZipcode(inStringMap
                                      .get(InCityStateZip.FIELD_ZIPCODE));
                      newInCityStateZip.setLatitude(inStringMap
                                      .get(InCityStateZip.FIELD_LATITUDE));
                      newInCityStateZip.setLongitude(inStringMap
                                      .get(InCityStateZip.FIELD_LONGITUDE));
                      newInCityStateZip.setCity(inStringMap.get(InCityStateZip.FIELD_CITY));
                      newInCityStateZip.setProvince(inStringMap
                                      .get(InCityStateZip.FIELD_PROVINCE));
                      newInCityStateZip.setCounty(inStringMap
                                      .get(InCityStateZip.FIELD_COUNTY));
                      newInCityStateZip.setZipclass(inStringMap
                                      .get(InCityStateZip.FIELD_ZIPCLASS));
      
                      if (isUnitedState(inStringMap.get(InCityStateZip.FIELD_PROVINCE))) {
                              newInCityStateZip.setCountry("United States");
                      }
                      
                      em.persist(newInCityStateZip);
                      recCount++;
              }
      



      When I use:


      InCityStateZip newInCityStateZip = new InCityStateZip();
      



      all the records are saved to the database.


      If I use:


      InCityStateZip newInCityStateZip = (InCityStateZip) Component.getInstance("newInCityStateZip");
      



      only the last record is saved. The newInCityStateZip is defined on the InCityStateZip entity with the scope type of Event.



      Does anyone know why only one record is being saved? If I shouldn't be using the 2nd way using the Component.getInstance(), then when should I use this?

        • 1. Re: Action Bean using Component.getInstance()
          mkiel

          In every loop iteration, Component.getInstance() returns the same object reference. When you call em.persist() for the first time, the persistence provider remembers this object by adding it to the persistence context. Any subsequent calls to em.persist() have no effect, since the argument is an already persistent object, and any changes to your object are applied to the one and only database row.


          You should check out the Hibernate reference chapter on batch processing. If you want to use scrolling, you probably will have to access the Hibernate session directly, which is explained in the Seam reference.


          You shouldn't have to use Component.getInstance() often, since injection usually does the job best. I would say the most interesting use case for Component.getInstance() are static methods as these can't be intercepted by Seam. But again, static methods shouldn't appear too often in a Seam application.

          • 2. Re: Action Bean using Component.getInstance()
            sandman202

            Marcel, thanks! That makes sense. I'll go back and reread the sections.