5 Replies Latest reply on Feb 11, 2009 9:37 AM by Amit Bafna

    Need help with setting a Session property

    Amit Bafna Newbie

      Hi All,


      I am trying to set a property for entire user session. The property is available as a drop down from the top of the web page, giving the user capability to change the property anytime. The drop down values are displayed fine but I am unable to change the property. Here is the code:


      components.xml


      <framework:entity-query name="sessionLocationsQuery" scope="SESSION"
          ejbql="select l from Location l"
          order="l.name"/>
      
      <factory name="sessionLocations" value="#{sessionLocationsQuery.resultList}" scope="SESSION"/>
      



      XHTML code


      <h:form id="sessionLocationForm">
          <s:decorate id="sessionLocationDecoration" template="edit.xhtml" rendered="#{identity.loggedIn}">
              <ui:define name="label">Session Location</ui:define>
              <h:selectOneMenu id="sessionLocationDropDown" value="#{locationSelection.location}">
                  <s:convertEntity />
                  <s:selectItems var="location" value="#{sessionLocations}" label="#{location.name}" />
                  <a4j:support ajaxSingle="true" event="onchange"/>
              </h:selectOneMenu>
          </s:decorate>
      </h:form>
      



      Session Bean


      @Stateful
      @Name("locationSelection")
      @Scope(ScopeType.SESSION)
      @Startup
      public class LocationSelectionBean implements LocationSelectionLocal {
          @Logger
          private Log                 log;
          
          @Create
          public void create() {
              log.debug("Created");
          }
          
          @Destroy @Remove
          public void destroy() {
              log.debug("Destroyed");
          }
          
          @In
          private Identity            identity;
          
          @In
          private EntityManager       entityManager;
          
          @In (create=true)
          private List<Location>        sessionLocations;
          
          private Location              location;
          
          public Location getLocation() {
              if (location == null) {
                  this.location = sessionLocations.get(0);
              }
              
              return this.client;
          }
      
          public void setLocation(Location location) {
              this.location = location;
          }
      }
      


      When I try to select an alternate location, the value is not updated in the session bean and the log reports following error:


      16:38:21,998 INFO  [lifecycle] WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
      sourceId=sessionClientForm:sessionClientDecoration:sessionClientDropDown[severity=(ERROR 2), summary=(value is not valid), detail=(value is not valid)]
      



      I even tried to change the scope of EntityConverter to SESSION as follows:


      <component name="myEntityConverter" class="org.jboss.seam.ui.converter.EntityConverter" scope="SESSION"/>



      and changed the XHTML code as follows:


      <h:form id="sessionLocationForm">
          <s:decorate id="sessionLocationDecoration" template="edit.xhtml" rendered="#{identity.loggedIn}">
              <ui:define name="label">Session Location</ui:define>
              <h:selectOneMenu id="sessionLocationDropDown" value="#{locationSelection.location}">
                  <f:converter converterId="myEntityConverter"/>
                  <s:selectItems var="location" value="#{sessionLocations}" label="#{location.name}" />
                  <a4j:support ajaxSingle="true" event="onchange"/>
              </h:selectOneMenu>
          </s:decorate>
      </h:form>
      



      Still got the same error.


      What am I missing? Any pointers will be appreciated.


      Thanks


      Amit

        • 1. Re: Need help with setting a Session property
          Amit Bafna Newbie

          I am working on JBoss 4.2.2 with Seam 2.0.2 and Richfaces 3.2.2


          Thanks


          Amit

          • 2. Re: Need help with setting a Session property
            Amit Bafna Newbie

            Return value for getLocation method was typo, Here is the correct getter/setter for the property


                public Location getLocation() {
                    if (location == null) {
                        this.location = sessionLocations.get(0);
                    }
                    
                    return this.location;
                }
            
                public void setLocation(Location location) {
                    this.location = location;
                }


            • 3. Re: Need help with setting a Session property
              Stuart Douglas Master

              For this to work you would need to have a SESSION scoped entity manager, which is not a good idea. <h:selectOneMenu > and similar require a converter to return an object that is equal to (i.e. object.equals()) to an object in the list. What is happening in your case is your session scoped list is created in one entityManager, which is then closed and you get a list of detached entities in the list box. Because entityConverter cannot return a detached entity, the object returned will be a new entity and will not be in the list and hence not be valid. You have three possible approaches to solving this problem:


              1) Override .equals and .hashCode for your entity to do PK equality, this has its own pitfalls that are well documented on the hibernate website.


              2) Change your list to use the same scope as your entityManager. This may result in an extra SQL call every now and then, however it should be taken care of by the second level cache. This is the easiest solution, I am not 100% sure it will work though.


              3) Create a custom converter that only selects items from the sessionLocationsQuery list. This is probably the most correct way.

              • 4. Re: Need help with setting a Session property
                Amit Bafna Newbie

                Thanks Stuart for quick response and detailed explanation. Can you please elaborate a bit more on option 3?


                Thanks


                Amit

                • 5. Re: Need help with setting a Session property
                  Amit Bafna Newbie

                  OOPS!!! Nevermind. I figured it out. It was something silly on my part. Thanks for the help.


                  Thanks
                  Amit