5 Replies Latest reply on May 1, 2007 1:23 PM by jonathan.shin

    How: Create multiple sub-entities in one single form

    thephil

      We are currently trying out Seam with quite some enthusiasm. Still, we are often running into time-consuming dead-ends.

      One of them:

      How do you create a main entity (e.g. a configuration) with multiple sub-entities (e.g. the settings of the configuration in multiple systems) using only ONE SINGLE form?

      The generated CRUD application (we used seam-gen) allows to create each of the sub-entities one-by-one, but not all of them at once. This would be what we need. We don't want to provide one single form as we only have a limited number of possible sub-entities.

      We'd be thankful for your input, looking forward to it.

        • 1. Re: How: Create multiple sub-entities in one single form
          gavin.king

          Its perfectly possible. Trivial in fact ... just put inputs for all the sub-entities in the main form. And make sure you instantiate the subentities when you instantiate the parent.

          Easy.

          • 2. Re: How: Create multiple sub-entities in one single form
            thephil

            Thanx for your immediate reply, Gavin! We really appreciate the work you devote to Seam :)

            I really tried to work this out, but could not manage. Here is what I did:

            1. @DataModel for the list of sub-entities.
            2. In the get method for the sub entities in the action listener (which is returning the list outjected with @DataModel), I fill the list of existing sub-entities with empty ones as required (see bottom of post for explanation)
            3. One sub-entity per factory is displayed (also if it did not exist yet)
            4. I enter something into the input field (JSF inputText tag in data table)

            Until now, everything worked.

            5. I execute the update method. Whatever I input as a value for the sub-entity is not saved to the DB. A log output shows that the actionlistener update method receives a main entity object with an empty (size = 0) list of sub-entities for the update.

            Do I have to manually link the @DataModel list to the main entity? I did not think so...

            Your help is greatly appreciated. Any hints? I am happy to supply source code snipplets if required, just did not want to crowd the post with it yet.

            @2:
            The data structure is: parameter > settings of the parameter > factories (a parameter has settings, one for each factory where it exists). The number of factories is constant. A parameter may not have a setting in a factory yet. We would like to edit/update the parameter settings together with the other parameter attributes using a single screen.

            • 3. Re: How: Create multiple sub-entities in one single form
              gavin.king

              So you are trying to make new objects persistent? Then you need to call persist(), or you need to add them to a collection with cascade=PERSIST.

              • 4. Re: How: Create multiple sub-entities in one single form
                thephil

                I managed to persist the subentities now. I had made the following mistake:
                I had returned a new list of sub-entities in the generator method for the DataModel. Now I returned a direct reference to the list hosted by the parent entity (instance.getParameterSettings after instance.setParameterSettings(newList)).

                This is great. However, there is one small thing left to ask.

                As I am displaying a list of sub-entities that do not yet have to exist, Seam creates the whole list of sub-entities when I do em.merge. Even those where I did not enter any values. This is understandable. However, we would need those sub-entities where nothing is entered NOT to be created.

                I tried to remove those sub-entities from the list (iterate over the list in the update method, check for setting stringlenght = 0, removed this item from list), but even though the new list (which i have saved to the parent entity using instance.setParameterSettings(newList)) has less entries, ALL sub-entities are present in the DB after the em.merge.

                How does Seam know and persist the sub-entities although I have removed them? Can I tell Seam not to persist certain sub-entities? Where would I have to remove them from the list?

                • 5. Re: How: Create multiple sub-entities in one single form
                  jonathan.shin

                  What do you mean by


                  just put inputs for all the sub-entities in the main form. And make sure you instantiate the subentities when you instantiate the parent.


                  Do you have any simple example/ view and bean code?

                  Thanks