1 Reply Latest reply on Jan 30, 2008 6:38 AM by pmuir

    Using OneToMany with SEAM and JSF

    rgbg5

      Hello,

      i am totally new to SEAM and JSF, so i hope you dont mind if this is a stupid question or posted to the wrong forum. I just couldnt find a example for this.

      I have a Entity class called Contacts which looks like this

      
      package gfm.entity;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.Lob;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import org.hibernate.annotations.CascadeType;
      import org.jboss.seam.annotations.Name;
      @Entity
      @Name("contact")
      public class Contact implements Serializable {
       private static final long serialVersionUID = 1L;
      
       @Id
       @org.hibernate.annotations.GenericGenerator(name="uuid_gen", strategy="uuid")
       @GeneratedValue(generator="uuid_gen")
       private String puid;
      
       private String title;
       private String firstName;
       private String middleName;
       private String lastName;
      
       private String company;
       private String department;
      
       @Temporal(TemporalType.DATE)
       private Date birthday;
      
       @Lob
       private String notes;
      
       @org.hibernate.annotations.CollectionOfElements
       @org.hibernate.annotations.IndexColumn(name="idx")
       @org.hibernate.annotations.Cascade(CascadeType.DELETE_ORPHAN)
       @JoinColumn(name="contact_puid")
       private List<StreetAddress> streetAddresses = new ArrayList<StreetAddress>();
      
       @org.hibernate.annotations.CollectionOfElements
       @org.hibernate.annotations.IndexColumn(name="idx")
       @org.hibernate.annotations.Cascade(CascadeType.DELETE_ORPHAN)
       @JoinColumn(name="contact_puid")
       private List<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();
      
       @org.hibernate.annotations.CollectionOfElements
       @org.hibernate.annotations.IndexColumn(name="idx")
       @org.hibernate.annotations.Cascade(CascadeType.DELETE_ORPHAN)
       @JoinColumn(name="contact_puid")
       private List<CommNumber> commNumbers = new ArrayList<CommNumber>();
      
       // Getter and setter .....
      
      }
      


      Each contact could have an unlimited number of addresses, emailaddresses and phone and faxnumbers.

      The question is, how do i reference a single streetaddress from within a JSF Page ? and how do i add new streetadresses to contact ? I hope i explained that right.

      An example or code snippet would be great.

      Thank you very much,

      Stefan

        • 1. Re: Using OneToMany with SEAM and JSF
          pmuir

           

          The question is, how do i reference a single streetaddress from within a JSF Page ?


          If you know which one try:

          #{contact.streetAddresses[10].zip}


          Or, iterate over them in a datatable.

          and how do i add new streetadresses to contact ?


          You need a method on your backing (action) bean like

          public void addStreetAddress() {
           contact.addStreetAddress(new StreetAddress());
          }