0 Replies Latest reply on Jun 8, 2010 2:39 PM by Michal Petrik

    Ajax submit and transaction

    Michal Petrik Newbie

      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 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"/>
             <rich:message id="lim" for="li" />
         <br />
          <a4j:commandButton id="edi" value="#{profile.editMode ? messages.ok : messages.edit}" 
                reRender="edu" action="#{profile.setEditMode(!profile.editMode)}"/>

      And this is backing POJO bean:

      public class ProfileBean implements Serializable{
           UserQuery userQuery;
           EntityManager entityManager;
           private User user;
           private String username;
           private boolean editMode = false;
           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:

      public void switchToEditMode(){
         editMode = true;
      public void switchToReadMode(){
          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.