7 Replies Latest reply on Dec 18, 2008 3:23 PM by rodrigotico

    why ejb function is trying to flush?

    rodrigotico

      I am using Seam 2.1.1.CR1, Jboss AS 4.2.3, seam managed persistence context and a seam transaction manager (ejb-transaction).


      I am on a long running conversation (A stateful EJB with flushMode MANUAL)


      On a conversation I get a persistence object: for example a person that has a relationship with orders (1..n)





      @Begin(join=true, flushMode=FlushModeType.MANUAL)
      public String createOrder() {
      ...
      Person p = entityManager.createQuery("from Person where id=1").getSingleResult();
      Order o = new Order();
      p.getOrder().add(o);
      o.setPerson(p);
      ...
      return "orderPage.xhtml";
      }
      





      orderPage.xhtml contains an ajax button that calls a method in the same conversation, that uses the same injected entityManager and runs a query, for example:



      @Begin(join=true, flushMode=FlushModeType.MANUAL)
      public void pressButton() {
      entityManager.createQuery("from Person").getResultList();
      }
      



      The problem is that when I press the button I am getting a TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing;


      I know that person is dirty (on JPA) with an unsaved transient instance (order), but my question is: why pressButton is trying to flush if is on MANUAL mode?

        • 1. Re: why ejb function is trying to flush?
          joblini

          Hi Rodrigo,


          You should call Persist before running the Query in pressButton. 


          • 2. Re: why ejb function is trying to flush?
            joblini

            You are querying the same collection (Person) which contains the transient instance, right?  So what should the entityManager do?  Overwrite your transient instance?  Looks like it is reminding you to save it before doing the Query ...


            • 3. Re: why ejb function is trying to flush?
              rodrigotico

              Hi ingo, thanks for reply!


              I just used from Person on pressButton query as an example, but a query on any other entity does the same behavior.


              Actually, there is a lot of workarounds that I can use, flush the session before run the query is one, don't set person.getOrder().add(o) at that moment is another.


              But what I am trying to understand is if this is the expected behavior.


              I don't think so, because the conversation is in manual flushmode and I am using seam managed persistence context and a seam transaction manager (ejb-transaction).


              I don't understand why Seam is trying to flush. Do you?


              Thanks!


              • 4. Re: why ejb function is trying to flush?
                rodrigotico

                Hi!


                Please, someone can explain this behavior? Is the expected?


                Thanks

                • 5. Re: why ejb function is trying to flush?
                  rodrigotico

                  Hi again! Anyone could help me with this question?

                  • 6. Re: why ejb function is trying to flush?
                    joblini

                    Hi Rodrigo,


                    Please refer to the following documentation, I think that it explains why flush is being called.


                    3.9. Flush the persistence context
                    3.9.1. In a transaction
                    
                    From time to time the entity manager will execute the SQL DML statements needed to synchronize the data store with the state of objects held in memory. This process, flush, occurs by default (this is Hibernate specific and not defined by the specification) at the following points:
                    
                        *
                    
                          before query execution*
                        *
                    
                          from javax.persistence.EntityTransaction.commit()*
                        *
                    
                          when EntityManager.flush() is called*
                    
                    (*) if a transaction is active


                    • 7. Re: why ejb function is trying to flush?
                      rodrigotico

                      yeah!!! Thanks very much for the perfect answer. I didn't know that hibernate
                      flushs the entity manager before any query!


                      I know this is a JPA/Hibernate issue, but i think this should be explained on seam reference doc (chapter 9.2. Seam managed transactions).


                      I created a new jira JBSEAM-3857