2 Replies Latest reply on Jul 12, 2010 7:38 PM by shavo26

    FlushModeType

    fjkjava.febinjk.gmail.com

      Hi All,



      I have a parent and child object, child stores parent object's id also. I have an excel file that contais data for both parent and child. I am trying to insert these values to database. If I use FlushModeType.MANUAL,


      for loop (Reading data from excel)
      {


      First Save Parent


      ?? Do i need to call flush Here to get Parent ID (Uses sequence to generate ID)


      Set Parent.id to Child.parent


      Then Save Child


      ?? OR call flush Here


      }


      OR call flush Here


      OR Dont use FlushModeType.MANUAL ? Please suggest


      Please help I am confused I tried everything but the application is running very slowly


      I know this is a stupid question but I am confused.


      Thanks

        • 1. Re: FlushModeType
          herberson

          In cases like yours I call flush after the loop, like this:





          for (read Parent from excel)
          {
              entityManager.persist(Parent)
              
              for (read Child from Excel)
              {
                  entityManager.persist(Child)
              }
          }
          entityManager.flush()
          









          • 2. Re: FlushModeType
            shavo26

            This scenario depends on how your transactions are managed and whether or not your your persistence context is scoped to conversation.




            Seam reference documentation states:
            By default, the persistence context is flushed (synchronized with the database) at the end of each transaction.
                This is sometimes the desired behavior. But very often, we would prefer that all changes are held in memory and
                only written to the database when the conversation ends successfully. This allows for truly atomic conversations.


                 
            If i understand this correctly its 1:N relationship



            //First persist parent
            entityManager.persist(Parent)
            //Set parent id on child
            child.setParent(Parent)
            //Persist the child
            entityManager.persist(Child)
            //At the end of conversation then flush your changes
            entityManager.flush()






            Take a look at:
            Seam-managed persistence contexts and atomic conversations
            here


            Also read hibernate entity manager reference documentation
            here