0 Replies Latest reply on Jun 8, 2010 2:39 PM by petrik.mich

    Ajax submit and transaction

    petrik.mich

      Hi,
      could anybody explain how it works, when I submit a form with an a4j:commandButton?


      I have following scenario:


      Page with edit button which should switch between read and edit mode.


      <h:form id="edu">
                     
        <a4j:region id="rd" rendered="#{not profile.editMode}">
           <p><h:outputText value="#{user.name}" /></p>
        </a4j:region>
      
        <a4j:region id="ed" rendered="#{profile.editMode}">
           <h:outputLabel for="li" value="#{messages.name}:"/>
           <br />
              <h:inputText id="li" value="#{user.name}" required="true">
                <rich:ajaxValidator event="onblur" eventsQueue="edu" ignoreDupResponses="true"/>
           </h:inputText>
             <rich:message id="lim" for="li" />
         </a4j:region>
         <br />
          <a4j:commandButton id="edi" value="#{profile.editMode ? messages.ok : messages.edit}" 
                reRender="edu" action="#{profile.setEditMode(!profile.editMode)}"/>
      </h:form>
      



      And this is backing POJO bean:




      @Name("profile")
      @Scope(ScopeType.PAGE)
      public class ProfileBean implements Serializable{
              @In
           UserQuery userQuery;
           
           @In 
           EntityManager entityManager;
           
           private User user;
           
           @RequestParameter
           private String username;
           
           private boolean editMode = false;
           
           @Create
           public void loadUser(){
                user = userQuery.getUser(username);
           }
      
              public User getUser(){
                     return this.user;
              }
      
              public void setEditMode(boolean editMode) {
                this.editMode = editMode;          
           }
           
           public boolean isEditMode() {
                return editMode;
           }
      
      }
      


      Everything works well but when I'm in edit mode, hit the ok button, the changes made to user entity are not flushed to database.


      I have also tried this approach:




      
      @Begin(flushMode=FlushModeType.MANUAL)
      public void switchToEditMode(){
         editMode = true;
      }
      
      @End
      public void switchToReadMode(){
          entityManager.flush();
          editMode = false;
      }
      
      



      but it did not help.


      I read documentation and understand that seam commits transaction after each request. But how it works, when there is an AJAX request?


      Thanks in advance.


      Michal