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

    Action Bean using Component.getInstance()

    scott duke Novice

      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?