1 Reply Latest reply on Mar 20, 2004 9:04 PM by semi

    calling entity finder method from session bean

    Jiri Chaloupka Newbie

      Hallo,
      I am learning ejb and I wrote some small application, which has stateless session bean, which works with bmp entity bean and cmp message driven bean.

      So, from session bean I am calling finder method of entity bean. This method looking for users which data in some table is not validated yet, somethig as:
      select distinct dealerid from sells where stateid = 4

      I tried to create PK bean, but IDE do not allow it, it allows only String (or collection of String as return type - it is correct in BMP or not?).

      So, method returns collection of string with values "dealerid" from database.
      In session bean, I have:

      Collection users = lh.findUsersToValidate();
      for(Iterator it = users.iterator(); it.hasNext(); ){
      String user = (String)it.next().toString();
      LocalSells sells = lh.findByPrimaryKey(user);
      ResultBean bean = sells.validateUsersSells(new UserPK(user));
      }

      but, as "user" value, I have there somethig as "local/sells:2195".

      This code works, becouse in validateUsersSells() method I am get the "dealerid" from "user"
      (something as :
      public int toUserId(){
      String[] _p = userId.split(":");
      return Integer.parseInt(_p[1]);
      }
      )
      but I know it is not good.

      What is he correct way to work with it?
      The next thing, I am calling findByPrimaryKey wit user parameter, which sets the dealerid to private variable in entity bean, but in next business method, in this variable is the first value, as:

      LocalSells sells = lh.findByPrimaryKey("local/sells:0001");
      ResultBean bean = sells.validateUsersSells(new UserPK("local/sells:0001"));

      - in sells the variable dealerid is set to 0001

      in next step, I call
      LocalSells sells = lh.findByPrimaryKey("local/sells:0002");
      ResultBean bean = sells.validateUsersSells(new UserPK("local/sells:0002"));

      and in sells is this variable setts still to 0001. Why? is it final variable (it is not defined as final manualy) or why?

      Thanks,
      Jiri

        • 1. Re: calling entity finder method from session bean
          semi Newbie

          Is the following code a call to the finder method on the home-interface of the LocalSells-Bean?

          Collection users = lh.findUsersToValidate();


          If so then the result is a collection of SellsLocal-Objects.

          Your finder method returns a collection of pk objects (collection of String-Objects in your case)
          But the result of a call to the finder method is a collection of LocalSells-Objects.
          You don't need to invoke the findByPrimaryKey method for each of them. This is done by the container.

          I don't understand your code at all. Is SellsBean validating users (UserBean) or vice versa?
          They seem to use the same PK. ResultBean is using UserPK too?

          ...
          LocalSells sells = lh.findByPrimaryKey(user);
          ResultBean bean = sells.validateUsersSells(new UserPK(user));
          ...
          


          If the value of 'user' is the primary key of LocalSells then LocalSells knows it and you don't need to pass the PK to it.

          I would say, you are searching for users whos sells are not validated and want to validate them and return a result to the client. Aren't you?

          This is allways the same question. Should a 'parent'-entity know about it's children or not. In your case, should UserBen know SellsBean?
          I would prefer the following code:
          public Collection validateAllSells()
          {
           UserHome userHome = .....;
           Collection userList = userHome.findAllBySellsState(4);
           ArrayList validationResult = new ArrayList(userList.size());
           Iterator userIterator = userList.iterator();
           while(userIterator.hasNext())
           {
           ...
           validationResult.add(
           validateUserSells((UserLocal)userIterator.next())
           );
           ...
           }
           return validationResult;
          }
          
          private ValidationResult validateUserSells(UserLocal user)
          {
           SellsHome sellsHome = ....
           Collection usersSells = sellsHome.findAllByUserAndState(user.getID(), 4);
           ValidationResult validationResult = new ValidationResult(user.getID());
           Iterator sellsIterator = usersSells.iterator();
           while(sellsIterator.hasNext())
           {
           SellsLocal sells = (SellsLocal)sellsIterator.next();
           ... // do some validation and store the result in 'validationResult'
           }
           return validationResult;
          }
          

          The only dependency of UserBean on SellsBean is the finder method findAllBySellsState(int state)
          An other solution were iterating trough UserSells directly without to use UserBean at all (something like SellsHome.findAllByState(4)).

          Michael