10 Replies Latest reply on Aug 17, 2009 5:46 PM by Ralf H.

    Values are persisted without calling EntityManager.persist

    Ralf H. Newbie

      Hey Guys,


      I have the following scenario:


      I have a bean called EventManager:



      @Stateful
      @Name( value = "eventHandler" )
      @Scope( value = ScopeType.CONVERSATION )
      @TransactionAttribute( value = TransactionAttributeType.REQUIRED )
      public class EventHandler implements EventHandlerLocal{
      
          @PersistenceContext( type = PersistenceContextType.EXTENDED )
          private EntityManager entityManager;
      
          private Event event;
      
          // some getter, setter and actions
      }



      In this Bean I handle the manipulation of my Event Object.
      In the Edit-Mask, there are some rich:tabs. Each Tab is selected in Ajax OR Server mode. E.g. the user changes some values in Tab1 and switches to Tab2 and the values are submitted to the Event Object in the Bean. Until here i have no Problem.



      BUT, if the user pushes the Cancel-Button on one of the Tabs, the values are stored in Datebase and I cant imagine WHY???
      What should the Cancel-Action do, to rollback changes the user made???


      I experimented with


      @Begin( nested = true, flushMode = FlushModeType.MANUAL )



      and




      @End
      public void save( ){
        this.event = this.entityManager.merge( this.event );
        this.entityManager.flush();
      }
      
      
      @End
      public void cancel( ){
        // what to do here, to rollback changes the user made???
      }





      to flush my EntityManager after the call of EntityManager.persist. But nothing works.



      Is there any method OR annotation I have to use???



      Thanks in advance,


      Ralf







        • 1. Re: Values are persisted without calling EntityManager.persist
          Leo van den berg Master

          A bit difficult to answer without the page which contains your cancel button, but I suspect  you are using ajax to validate and you should disable updating the database there by setting the bypassUpdates attribute to true. So possibly it has nothing to do with the cancel-button, because the values are already updated.


          • 2. Re: Values are persisted without calling EntityManager.persist
            Ralf H. Newbie

            Hi,


            thank you for your fast reply.


            Sure, i have some AJAX actions (e.g. validation), but the Cancel-Button is a normal h:commandButton.


            I will try the ByPassUpdate attribute...


            But nevertheless, i think the main problem is the Tab-Switch. On a switch, all values from the current Tab are submitted AND this will cause an update of the values in the Event object in the Conversational-Bean EventHandler.


            How to handle this?! :)

            • 3. Re: Values are persisted without calling EntityManager.persist
              Leo van den berg Master

              try the bypassUpdates. The tab-panel shouldn't submit just because you change the tab (would be a very tricky application)


              • 4. Re: Values are persisted without calling EntityManager.persist
                Ralf H. Newbie

                i think we probably dont discuss the same subject...
                i mean the rich:tabpanel


                there the values are submitted :)

                • 5. Re: Values are persisted without calling EntityManager.persist
                  Ralf H. Newbie

                  I tried to edit only one value, which is just a h:inputtext and after a rich:tab-switch the values are persisted.


                  Is there a way, where i can say EntityManagaer.startTransaction and EntityManagaer.rollback/commit.
                  I know, that i can use an other Transaction Manager and probably this will work, but in my opinion i break the Seam/EntityManager concept...

                  • 6. Re: Values are persisted without calling EntityManager.persist
                    Leo van den berg Master

                    Hi Ralf,


                    We are talking about the same rich:tabpanel. I use it with a single entity split over different tabs and it doesn't produce a persiste or update when I swith the tabs. I use a ajax-switchtype and ajax for validate and submit. Works fine for me. Send the relevant part of the tab to see if there is something strange happening.




                    • 7. Re: Values are persisted without calling EntityManager.persist
                      Ralf H. Newbie

                      I will shorten the page a little bit ...


                      I just have an idea, ... the h:form is the parent of the rich:tabpanel. Maybe this causes the problem?!



                      @Out( required = false )
                      private Event currentEvent;




                      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                                            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                                      xmlns:s="http://jboss.com/products/seam/taglib"
                                      xmlns:ui="http://java.sun.com/jsf/facelets"
                                      xmlns:f="http://java.sun.com/jsf/core"
                                      xmlns:h="http://java.sun.com/jsf/html"
                                      xmlns:rich="http://richfaces.org/rich"
                                      xmlns:a4j="http://richfaces.org/a4j"
                                      template="layout/div/hauptseite.xhtml"
                              >
                      
                          <ui:param name="wobinich" value="Eventmanager" />
                      
                          <ui:define name="inhalt">
                              <h:form id="eventform" enctype="multipart/form-data">
                                  <h:messages globalOnly="false" styleClass="message"/>
                                  <rich:tabPanel switchType="ajax" >
                                      <rich:tab label="Event">
                                          <s:validateAll>
                                              <h:inputText id="dfResult" value="#{currentEvent.name}" />
                                          </s:validateAll>
                                      </rich:tab>
                                      <rich:tab label="Details">
                                          <s:validateAll>
                                              <h:inputText id="anotherField" value="#{currentEvent.costs}" />
                                          </s:validateAll>
                                      </rich:tab>
                                  </rich:tabPanel>
                              </h:form>
                          </ui:define>
                      </ui:composition>





                      • 8. Re: Values are persisted without calling EntityManager.persist
                        Ralf H. Newbie

                        I made a form in every tab, now i have the problem, that i loose changed information on a tab-switch... ARGH!

                        • 9. Re: Values are persisted without calling EntityManager.persist
                          Leo van den berg Master

                          Your validate-all tag is probably the cause. To validate, your model has the new inserted values. Place the tabpanel as a whole between the form tags and make sure you don't have nested form-tags (form in a form).



                          • 10. Re: Values are persisted without calling EntityManager.persist
                            Ralf H. Newbie

                            It doesnt work...


                            I removed all s:validate tags and made just one form... on tab-switch, the values are persisted.


                            I am thinking about a long-running-transaction...