0 Replies Latest reply on Nov 17, 2006 6:31 AM by toni

    OneToOne Bidirectional

    toni

      Hi,

      I have a short question about establishing a relationship between, for example, a Person and an Address Object, in which the Person Object references the Address Object exactly TWICE.

      The Person Object contains two field properties each referencing an Address Object. The first one is the home address and the second one the work address, but they are essentially the same Class.

      I have problems setting this up and I wonder, if it is possible at all. I know I could use a one-2-many or many-2-many relationship, but it's my intention not to do so. I explicitly want there to be two Address field properties within the Person Object.

      Here the short version of the code:

      @Entity
      @Name("person")
      @Scope(SESSION)
      public class Person
      {
      @Id
      @GeneratedValue
      long id;

      @OneToOne(cascade={CascadeType.ALL})
      Address homeAddress;
      @OneToOne(cascade={CascadeType.ALL})
      Address workAddress;

      ...
      }

      Referencing the Address Object seems to be not a problem, even though I'm not sure about this.

      @Entity
      @Name("address")
      @Scope(SESSION)
      public class Address
      {
      @Id
      @GeneratedValue
      long id;

      @OneToOne(mappedBy="???")
      Person personOwning;

      ...
      }

      However, I definetly run into problems, when it comes to referencing back to the Person Object. What do I put in "mappedBy" ???

      It can't be "homeAddress" or "workAddress", so it should be "id" of Person, but that's an Integer, which seems strange.

      From the database point of view this is not a problem at all - the whole mapping I'm trying to set up - but the same mapping seems not to be workable for EJB3?