13 Replies Latest reply on Apr 2, 2008 2:59 PM by pmuir

    Problems with AJAX and update model

    luisfelipe

      Hello Everybody !


      I am having problems with Ajax in the following situation:


      I want to update some fields in my page with a a4j:support in a h:selectOneMenu, my entity is being updated in the event AJAX and It's persisted.
      For my application should not happen, because if user to cancel the operation, the entity should be with its previous state.
      Someone has any solution to this problem?


      I am using Jboss Seam 2.0 with Rich Faces 3.1.2.GA



      Thanks!
       

        • 1. Re: Problems with AJAX and update model
          tom_goring

          1) Use richface attribute

          bypassUpdates="false"

          to not update your domain model


          2) Make sure you have

          FlushModeType.MANUAL


          EG


          @Begin(flushMode=FlushModeType.MANUAL)
          



          Read Seam doco about that

          • 2. Re: Problems with AJAX and update model
            tom_goring

            that should have read


            bypassUpdates="true"
            



            I guess it's the flush mode that is your main problem as you are dealing with an attached entity.

            • 3. Re: Problems with AJAX and update model
              alessandro

              Tom Goring wrote on Mar 10, 2008 11:58 PM:


              that should have read

              bypassUpdates="true"
              



              I guess it's the flush mode that is your main problem as you are dealing with an attached entity.



              I have the same problem and I do not know the answer.


              Using:

              bypassUpdates = "true"

              ...isn't appropriate, because the fields that are in the reRender don't show updated (And this is important when using AJAX).


              Using:

              flushMode = "manual"

              ...also does not solve the problem.
              The model is not persisted in dataBase, but if you click a button like Cancel (with immediate true), the object is still to the state in memory modified by Ajax. If another page to display the object, it will be with wrong values.


              Somebody has some tips?

              • 4. Re: Problems with AJAX and update model
                pmuir

                I don't understand. You want to update the values on the page without updating the entity values? You can't do this with JSF/Ajax4jsf.

                • 5. Re: Problems with AJAX and update model
                  damianharvey.damianharvey.gmail.com

                  So the problem you have now (using flush=manual) isn't that the Entity is updated in the database, it's that the Entity has the new (wrong) values when it is displayed on a subsequent page?


                  If so why not call a refresh() from your cancel? Or end the conversation & start a fresh one on the next page.


                  Cheers,


                  Damian.

                  • 6. Re: Problems with AJAX and update model
                    alessandro

                    Pete Muir wrote on Mar 11, 2008 02:30 PM:


                    I don't understand. You want to update the values on the page without updating the entity values? You can't do this with JSF/Ajax4jsf.


                    I need update the values on the page, but if user click on the button Cancel, Back or others that not Save button, the model must discard the new values.

                    • 7. Re: Problems with AJAX and update model
                      damianharvey.damianharvey.gmail.com

                      If you use manual flush the model won't be updated to the Database until you call entityManager.flush().


                      If you call a refesh() on your Cancel() or Back() then the model will be reloaded from the database.


                      Cheers,


                      Damian.

                      • 8. Re: Problems with AJAX and update model
                        alessandro

                        Hi Damian ...


                        I have done some tests with flushMode manual, but without success.
                        The data are not recorded in the database, but I believe that the modified objects are cached, as on other pages it appears modified.


                        Even if I click the button refresh on browser on other pages, the values appear incorrect (but in the database are correct).


                        The situation is:


                             
                        -- An object A have the one attribute with List of objects B.
                        This attribute is the value of datatable on page1.xhtml.


                        The row of datatable is link for edit object B in page2.xhtml.


                        -- If I change the object B, with an event AJAX but not save, and click the h: commandButton Back, the page1.xhtml show the dataTable with a row modified.


                        -- In the database, the value is the old (fine!), but in page1.xhtml is the new (wrong) value of AJAX modified on page2. Even if I click the refresh browser, the value continues changed.


                        Do you have some idea of why this happens?
                        Thanks

                        • 9. Re: Problems with AJAX and update model
                          damianharvey.damianharvey.gmail.com

                          Hibernate has cached it. That's why you need to call an entityManager.refresh() (not a browser refresh) to reload the cached entity.


                          Cheers,


                          Damian.

                          • 10. Re: Problems with AJAX and update model
                            alessandro

                            :-)
                            This working!


                            I had confused with refresh, sorry.


                            This use like:
                            DataTable filter / Edit the object / Save-Cancel the object and use Ajax on the pages is very common. Does Seam along with the Hibernate could not have a new feature a bit more natural for these cases?


                            The s: button with implicit refresh or Partial Flush (with auto flush only in CRUD operations) would be very cool.


                            Well, thank you very much!
                                 
                                      

                            • 11. Re: Problems with AJAX and update model
                              luisfelipe

                              Thanks Alessandro, Damian and Pete!


                              My problem has been resolved and my application is running, thank collaboration to clarify my doubts!


                              Thank very much!

                              • 12. Re: Problems with AJAX and update model
                                alessandro

                                How to disable this cache of Hibernate?


                                This very difficult to control the refresh button, because the user may not click on the button (with refresh action in entityManager) and click another button to any page (or links).

                                • 13. Re: Problems with AJAX and update model
                                  pmuir

                                  Damian is describing the first level cache provided by the persistence context. Read a good book (like Christian's) on JPA.