3 Replies Latest reply on Apr 29, 2005 2:21 AM by ffray

    customer <- customer_address -> address and address type rel

    sonofseven

      hi there,

      Imagine we have 4 tables
      customer <- customer_address -> address & address_type
      how would I go and implement this with EJB3? Is it possible?
      I know that I can go one level down, but is it possible that I have just a class Customer that contains list of addresses of different type???

      many thanks
      rp

        • 1. Re: customer <- customer_address -> address and address type
          ffray

          Sure you can. If your address types should be an entity it would
          look like this:

          package crm;
          
          import javax.persistence.AccessType;
          import javax.persistence.Column;
          import javax.persistence.Entity;
          import javax.persistence.GeneratorType;
          import javax.persistence.Id;
          
          @Entity (access=AccessType.FIELD)
          public class AddressType {
          
           /**
           *
           */
           private static final long serialVersionUID = 3258135751752692024L;
          
           @Id(generate=GeneratorType.AUTO)
           private int id;
          
           @Column (length = 5)
           private String typeName;
          
           @Column (length = 50)
           private String description;
          }
          
          package crm;
          
          import javax.persistence.AccessType;
          import javax.persistence.Entity;
          import javax.persistence.GeneratorType;
          import javax.persistence.Id;
          import javax.persistence.JoinColumn;
          import javax.persistence.ManyToOne;
          
          @Entity(access = AccessType.FIELD)
          public class Address implements Serializable {
          
           /**
           *
           */
           private static final long serialVersionUID = 3258135751752692023L;
          
           @Id(generate = GeneratorType.AUTO)
           private int id;
          
           @ManyToOne
           @JoinColumn(name = "addrType_id", referencedColumnName = "id")
           private AddressType addressType;
          
           private String street;
          
           private String zip;
          
           private String city;
          
           private String country;
          }
          
          package crm;
          
          import java.util.Collection;
          
          import javax.persistence.AccessType;
          import javax.persistence.Entity;
          import javax.persistence.GeneratorType;
          import javax.persistence.Id;
          import javax.persistence.JoinColumn;
          import javax.persistence.OneToMany;
          
          @Entity(access = AccessType.FIELD)
          public class Customer implements Serializable {
          
           /**
           *
           */
           private static final long serialVersionUID = 3258135751752692025L;
          
           @Id(generate = GeneratorType.AUTO)
           private int id;
          
           private String name;
          
           @OneToMany(mappedBy = "customer_address")
           @JoinColumn(name = "cust_id", referencedColumnName = "id")
           private Collection<Address> addresses;
          
          }
          


          If you just flag the purpose of each address a customer has your code could look like the code shown above.
          If you have real new types (means classes) of addresses you can inherit address like:
          package crm;
          
          import javax.persistence.AccessType;
          import javax.persistence.Entity;
          
          @Entity (access = AccessType.FIELD)
          public class PrivateAddress extends Address {
          
           /**
           *
           */
           private static final long serialVersionUID = 3762257422173221171L;
          
           /* sth. useful here */
          }
          


          This code *SHOULD* work, just add usefule properties and the getters / setters.

          Hope it helps

          ff

          • 2. Re: customer <- customer_address -> address and address type
            epbernard

             

            "ffray" wrote:
            @OneToMany(mappedBy = "customer_address")
            @JoinColumn(name = "cust_id", referencedColumnName = "id")
            private Collection<Address> addresses;

            }

            This one is buggy, I think.
            Either you have a customer_address *property* on the address side and @Joincolumn shouldn't be here
            Or remove the mappedBy to make it the owner side.

            Plus a @ManyToMany is probably more appropriate in your case.

            • 3. Re: customer <- customer_address -> address and address type
              ffray

              Oh, my fault!
              You're right! But i wouldn't like a @ManyToMany here since this would mean two distinct customers could share an address.
              Well you could do that ... it depends on the usecase.
              Looking for maximum redundant-free design m:n is correct but brings problems when one customer is moving (don't update the address!) and when entering new addresses (first search for identical address - 100% or unsharp? -, then use the existing one, otherwise insert the entered).
              I don't know if this is what should be achieved. I think this should be a simple example.
              So nevermind about the errors, it was a quickshot with a slight incorrect look at the @OneToMany Example at the spec.

              Greetz

              FF