6 Replies Latest reply on Jun 9, 2007 1:37 PM by Pete Muir

    Building parent and child in one page

    Bieth Bruno Newbie

      I would like to create a questionnaire and its questions in the same page.
      The user will be able to add/remove/edit question to a new questionnaire and save/cancel the whole thing at the end.
      I have problems for editing a question. Here is the use case :
      -> New Questionnaire
      -> New Question (1)
      -> Save Question (1)
      -> Edit Question (1)
      -> Cancel changes on Question 1
      -> Cancel Questionnaire (everything is rolled back)

      I tried few things but I can't get both cancel phase working in a nice way.
      Any ideas ??

      This is what I tried :
      * A questionnaire editor start a conversation on a newQuestionnaire request.
      When the user add a question, I just add it to the questionnaire entity question collection.
      Problem when editing I can't rollback the user changes (cause the question object is used and modified on update model JSF phase).
      The only solution I see is cloning the editing question. But I don't like it.

        • 1. Re: Building parent and child in one page
          Pete Muir Master

          Use a MANUAL flushmode conversation

          • 2. Re: Building parent and child in one page
            Bieth Bruno Newbie

            I tried that one as well but it fails for this use case :
            -> New Questionnaire

            ----> New Question (1)
            ----> Save Question (1)

            ----> Edit Question (1)
            ----> Cancel changes on Question 1

            -> Save Questionnaire (em.flush())

            I got the changes done during the edit step.
            Thanks for the reply.

            • 3. Re: Building parent and child in one page
              Bieth Bruno Newbie

              Ok no one see a solution without cloning ?
              It would help me a lot !
              Thanks

              • 4. Re: Building parent and child in one page
                Pete Muir Master

                Try using nested conversations I think, you may also need use a two entities, one for the parent and one for the nested conversation, and be careful when loading entities. You'll still need manual flush mode. An alternative would be manually roll back changes on cancel, or to use detached entities. I pretty sure cloning isn't necessary.

                • 5. Re: Building parent and child in one page
                  Bieth Bruno Newbie

                  Thank for your reply Pete.

                  I tried nested conversation without success. The problem is that you get the same persistence context even if you use two components.

                  I didn't find a solution with JTA as well :
                  - Start a transaction with entity manager 1
                  - Persist the parent

                  - Start a transaction with entity manager 2
                  - merge the parent in the new persistence context
                  - create a child
                  - add it to the parent
                  - persist the child
                  - commit the transaction
                  - close entity manager 2

                  - rollback transaction (EM 1)
                  => parent is rolled back but the child is in the database because of the commit in the nested transaction.

                  Do you mean something different by "roll back changes on cancel" ?

                  How can you use detached entities to solve this problem ?

                  • 6. Re: Building parent and child in one page
                    Pete Muir Master

                    Ooops, sorry, I should have written

                    Try using nested conversations I think, you may also need use multiple entitiy managers, one for the parent conversation and one each nested conversation - be careful when loading entities that you get it from the correct one
                    .

                    This is probably best, but is perhaps hardest to get right IMO.

                    You don't want to do this using transaction AFAICS, I really meant you would do a reload of the entity from the PC in code, but I this is really fighting against the way hibernate works and would be a mess!

                    Detached entities would allow you to manually merge only the entities you wanted to save (along with the correct cascades) - but you loose all the benefits of the entity being managed. Perhaps this is most practical solution.