2 Replies Latest reply on Jan 26, 2005 1:56 PM by dsouza

    One-to-one relationship in EJB 3

    dsouza

      Hi,

      I'm having trouble creating a one-to-one relationship in an EJB 3 entity bean. I'm trying to create the relationship in a way that both sides will have the same primary key. The tutorial shows how to create the relationship in a way
      that each side has their own primary key and the relationship is defined by placing a foreign key on each side.

      The tutorial defines the one-to-one relationship between Customer and Address. This is what it looks like in Customer:

      @OneToOne(cascade = {CascadeType.ALL})
      @JoinColumn(name = "ADDRESS_ID")
      public Address getAddress()
      {
      return address;
      }

      This code creates a join column on the Customer table which is a foreign key to Address.

      In my case, I would like to not have that extra column in the Customer table and have Address use the same primary key as Customer.

      The EJB specification shows the following example:

      One-to-one association that assumes both the source and target share the same primary key values

      @OneToOne(optional=false)
      // No join column required
      public Customer getCustomer() { return customer; }


      I'm not having any success in getting that to work. So far my code looks like this:

      In Customer:
      @Id(generate = GeneratorType.AUTO)
      public Integer getId() {
      return id;
      }

      ...

      @OneToOne(cascade = {CascadeType.ALL}, optional = false)
      public Address getAddress() {
      return address;
      }


      In Address:
      @Id(generate = GeneratorType.AUTO)
      public Integer getId() {
      return id;
      }

      ...

      @OneToOne(cascade = {CascadeType.ALL}, optional = false)
      public Customer getCustomer() {
      return customer;
      }

      The problem I'm having is that the primary keys on each entity are being generated independently of one another which, given the way I defined the primary keys, makes perfect sense. I feel like it's missing something, though. The question that stays in my head is, how do I define the primary keys for each side of the relationship in a way that the container will manage it and know that both of them must be the same?

      I'm thinking this feature might not be implemented but I couldn't find anything that will confirm or deny that. Can anyone help me please?

        • 1. Re: One-to-one relationship in EJB 3
          bill.burke

          if this is not a bi-directional relationship, then you are better off using SecondaryTable and Dependent

          • 2. Re: One-to-one relationship in EJB 3
            dsouza

            Yes, I see how that could work, however, it would take a lot more code. I'd have to use a DependentAttribute annotation for every attribute in the object on the other side of the relationship making it more difficult to maintain. I wouldn't go that far just to save a column on a database table.

            Isn't there a better approach to this?