4 Replies Latest reply on Oct 16, 2007 1:38 AM by Mohammad Norouzi

    why Factory method is calling more than once?

    Mohammad Norouzi Novice


      I have a factory method for a field User in my SB, and I want to know why this method is calling as many time as the number of User's field?

      in my case, the User has 12 fields do this method is called 12 times whereas I load it once from database ?

      please take a look at following code:

       @Factory(value = "tempUser", scope = ScopeType.STATELESS)
       public User loadUser() {
       if(reload || tempUser == null) {
       if(selectedUsername.equals("0")) {
       logger.debug("instantiating a new User");
       tempUser = new User();
       verifyPassword = password = "";
       selectedGroup = 0L;
       logger.debug("loading User with username '"+selectedUsername+"'");
       tempUser = userDao.load(selectedUsername);
       if(tempUser.getGroup() != null)
       selectedGroup = tempUser.getGroup().getId();
       verifyPassword = password = tempUser.getPassword();
       selectedRoles = new ArrayList<String>();
       for(UserRole r : tempUser.getRoles()) {
       reload = false;
       return tempUser;

      as you see, at first the "reload" is true so the User will be populated from the database and then the reload will set to false, but at my XHTML page, no value will be displayed !!!!! however I can see all the User's value in the logs (produced by logger)

      if I remove the line highlighted bu blue color reload = false; it will work fine but this method will be called 6 times and in app server console I can see that 12 times the EJB is selecting from database!!!

      thank you very much in advance

        • 1. Re: why Factory method is calling more than once?
          Christian Bauer Master

          Use a stateful context, not STATELESS...

          • 2. Re: why Factory method is calling more than once?
            Mohammad Norouzi Novice

            Hi Christian,
            but if I alter the STATELESS to SESSION my problem will changed, because I have a combo box at the page and when the user change it the forms will be submitted and the information of the selected user should be displayed.

            if I use the SESSION scope for the factory method, at first the user is instansiating as "new User()" and because the session is still alive, this factory method won't be called until the session is destroyed or the user is null so this won't resolve the problem

            and if you meant changing my Session bean to a stateful context, I did it but problem still exists, it seems that being SFSB or SLSB has nothing to do with this factory method and the method will be called whenever the Seam needs it.

            you know, if I remove the relaod=false; from the code everything is fine but the problem is when user is going to update the information the submitted values won't apply to tempUser because after submitting new values, Seam is calling this factory method and everything is turning to its first step!!!!

            thank you in advance for your reply, any comment would be of a great help

            • 3. Re: why Factory method is calling more than once?
              Christian Bauer Master

              Factory methods are not the right way to do what you want to do. There are many other ways, one would be an a:support on your select box that calls a loadUser() method on a backing component, which sets the user variable in the PAGE context.

              • 4. Re: why Factory method is calling more than once?
                Mohammad Norouzi Novice

                I did what you suggest, but <a:support> doesn't resolve my problem.

                first off, after calling the loadUser() by a:support nothing will be displayed on page

                second, apart from that, when the user clicks on the Save button, the form is submitting but the thing is that (it seems) Seam doesn't populate an object inside a SB from the request, does it?
                after form submission, an exception is thrown that says the tempUser resolves to null, one way is to define instance variables in a SB instead of defining an object of type User but this makes your bean very untidy.
                maybe I am mistaken but I'd like to know your idea.

                class MySessionBean {
                 private User tempUser;
                 // rest of the codes...

                class MySessionBean {
                 private String username;
                 private String password;
                 private String userAddress;
                 private String name;
                 private String family;
                 // rest of the codes...