3 Replies Latest reply on Jan 27, 2009 4:45 AM by clerum

    Help with Updating a Session Entity Object

    clerum

      OK-


      In my application when a user logins in I outject an user Session object named authuser. This is the object I use to pull the logged in users organization, name, etc..This seems to work fine.


      I have new page which allows the user to edit their information.



      <h:form id="updateUserForm"> 
       
              <rich:panel> 
                  <f:facet name="header">Edit Profile</f:facet> 
                  <s:decorate id="usernameDecoration" template="/layout/edit.xhtml"> 
                      <ui:define name="label">Email Address</ui:define> 
                      <h:inputText id="username" required="true" value="#{authuser.username}"/>                 
                  </s:decorate>
                  <s:decorate id="firstnameDecoration" template="/layout/edit.xhtml"> 
                      <ui:define name="label">First Name</ui:define> 
                      <h:inputText id="firstname" required="true" value="#{authuser.firstname}"/>                 
                  </s:decorate>
                  <s:decorate id="lastnameDecoration" template="/layout/edit.xhtml"> 
                      <ui:define name="label">Last Name</ui:define> 
                      <h:inputText id="lastname" required="true" value="#{authuser.lastname}"/>                 
                  </s:decorate>
                  <s:decorate id="organizationDecoration" template="/layout/edit.xhtml"> 
                      <ui:define name="label">Organization</ui:define> 
                      <h:outputText id="organization" required="true" value="#{authuser.organization.name}"/>                 
                  </s:decorate>
                  <div style="clear:both"/>            
              </rich:panel> 
       
                <div class="actionButtons">
                  <h:commandButton id="update" value="Update" action="#{userutil.updateUser(authuser)}"/>
                  <h:commandButton id="cancel" value="Cancel" action="/home.xhtml"/>            
                   </div>
              
          </h:form> 





      public void updateUser(User selectedUser)
           {
                user = em.merge(selectedUser);
                em.persist(user);
              }



      This all appears to work however if the user tries to edit their information again then get an error about that row already being edited by another user. I can see from the debug page that the version differs between the database and the Session object authuser.


      How do I go about updating the authuser session bean?


      Is there a correct way to handle situations like this which would have the bean updated automatically?