0 Replies Latest reply on Apr 6, 2005 11:37 AM by guurk

    OneToMany and OneToOne w/ same tables/entities

    guurk

      Here's the setup:

      // desc salespersons
      id <- primary key
      name
      activeaccountid <- OneToOne
      
      //desc accounts
      id <- primary key
      sid
      


      There is a unidirectional OneToMany relationship that works great where there are multiple accounts rows for each salespersons row based on the accounts.sid = salespersons.id

      There is a second unidirectional OneToOne releationship that isn't working for me.

      @Entity
      @Table(name="salespersons")
      public class Salesperson implements Serializable {
      ...
      
      private Integer id;
      private String name;
      private Integer activeaccountid;
      
      private Collection<Account> _accounts;
      private Account _activeaccount;
      
      // This works GREAT!
      @OneToMany
      @JoinColumn(name="sid")
      public Collection<Account> getAccounts() {
       return _accounts;
      }
      public void setAccounts( Collection<Account> accounts) {
       _accounts = accounts;
      }
      
      // THIS IS WHERE I GET CONFUSED
      @OneToOne
      // I've tried this:
      @JoinColumn(name="id", referencedColumnName="activeaccountid")
      // And this:
      @JoinColumn(name="activeaccountid", referencedColumnName="id")
      // and all sorts of other combinations
      public Account getActiveAccount() {
       return _activeaccount;
      }
      public void setActiveAccount( Account activeaccount) {
       _activeaccount = activeaccount;
      }
      
      ... getters and setters for fields ...
      
      ...
      }
      


      So, how do I set up the OneToOne relationship? I already have the OneToMany relationship working.