0 Replies Latest reply on Jun 27, 2008 2:52 PM by jope

    Multiple (different) relations between two entities?

    jope

      I need to write an EntityBean that holds a collection of EntityBeans of another type plus two single references to EntityBeans of that type. A simple example would be a customer who has a work address and a home address, and a collection of previously used addresses that also need to be stored.

      The classes could look like this:

      @Entity
      public class Customer implements Serializable
      {
      private Integer id;
      ...
      private Address homeAddress;
      private Address workAddress;
      private Address[] previousAddresses;
      ...
      @CollectionOfElements
      @IndexColumn(name="index1")
      public Address[] getPreviousAddresses()
      {
       return previousAddresses();
      }
      
      @OneToOne
      public Address getHomeAddress()
      {
       return homeAddress;
      }
      
      @OneToOne
      public Address getWorkAddress()
      {
       return workAddress;
      }
      ...
      }
      
      @Entity
      public class Address implements Serializable
      {
      private Integer id;
      ...
      }
      

      However, I could not find any combination of annotations that would persist this setup in a sensible way. All the Addresses always get stored in one table, and I can't find a way to distinguish which address plays which "role" in the customer class when trying to write an SQL query to read them. I would especially like to avoid having to declare a Customer attribute in the Address class, since this would be redundant on the object level and would produce a lot of implementation overhead in my application.
      Does anybody know how this can be done? I'm sure I am not the first person running into this need, but unfortunately, all the books, docs and tutorials I found don't cover it.