3 Replies Latest reply on Sep 17, 2009 2:24 PM by tmalatinszki

    Capture all inputs related to OneToMany relationship in a single form

    evejeba
      I have a simple requirement where I need to capture all information regarding my OneToMany relationship in a single form. How do I generate a .xhtml page to handle creation / modification of entities.

      import java.util.List;

      @Entity
      @Table(name = "person")
      public class Person implements java.io.Serializable {

          @Id
          @GeneratedValue(strategy = IDENTITY)
          @Column(name = "id")
          private Integer id;

          private String name;

          @OneToMany(mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
          private List<Email> emails;

          public Person() {
          }

          public Integer getId() {
              return id;
          }

          public final void setId(final Integer id) {
              this.id = id;
          }

          public String getName() {
              return name;
          }

          public final void setName(final String name) {
              this.name= name;
          }

          public final List<Email> getEmails() {
              return emails;
          }

          public final void setEmails(final List<Email> emails) {
              this.emails= emails;
          }
      }

      Email.java just has a single member variable emailAddress.

      I would like the form to have a field for Person.name and 2 additional fields for Email.emailAddress.
      How do I generate a form which will dynamically bind my form inputs to the PersonHome, EmailHome classes. I don't want to use the OneToMany TAB which is generated for adding emails.

      Appreciate any pointers / help on this topic
        • 1. Re: Capture all inputs related to OneToMany relationship in a single form
          tmalatinszki

          Hi Evelyn,


          You shold use <a4j:repeat> in this case.


          <h:inputText value="#{person.name}"/>
          <a4j:repeat value="#{person.emails}" var="email">
            <h:inputText value="#{email.emailAddress}"/>
          </a4j:repeat>



          Regards,
          Tamas

          • 2. Re: Capture all inputs related to OneToMany relationship in a single form
            evejeba
            Thanks Tamas for your inputs.

            I would like to provide a static list of 3 input text fields for the user.
            The above code doesn't generate the text fields since the #{person.emails} is empty initially.
            I would like to somehow associate the above input text fields dynamically to emailHome.instance. How do I achieve that.
            • 3. Re: Capture all inputs related to OneToMany relationship in a single form
              tmalatinszki

              The above code doesn't generate the text fields since the #{person.emails} is empty initially.

              It's easy to handle: add two new Email entitites to emails list at the beginning, and the code will generate the two (initially empty) input fields for emails. The only thing You have to care about is removing empty Email entities before saving Person entity.
              I think it's a better solution than using any kind of static fields (if You want to store five emails instead two, You only need to change a single number in Your java code instead modifying xhtml+java code also), and doesn't requires extra temporary variables neither, because in this case all email input fields binded directly to Person entity's emails variable.


              Regards,
              Tamas