View Javadoc
1   package ejava.examples.orm.rel.annotated;
2   
3   import javax.persistence.*;
4   
5   import org.apache.commons.logging.Log;
6   import org.apache.commons.logging.LogFactory;
7   
8   /**
9    * The class provides a set of OneToOne relationship examples.
10   * It contains a uni-directional, non-primary key relationship to Person and a 
11   * bi-directional, non-primary key relationship with Borrower. In both cases
12   * the Applicant owns the relationship. This means that the foreign key forming
13   * the relationships will exist in the Applicant's table.
14   */
15  @Entity @Table(name="ORMREL_APPLICANT")
16  public class Applicant  {
17      private static Log log = LogFactory.getLog(Applicant.class);
18      @Id @GeneratedValue
19      private long id;
20      
21      @OneToOne(optional=false)       //we must have a Person
22      @JoinColumn(name="APP_PERSON")  //name of our foreign key column
23      private Person identity;
24      
25      @OneToOne(optional=true)        //we may exist without Borrower 
26      @JoinColumn(name="APP_BORROWER")//we own relationship to Borrower
27      private Borrower borrower;
28      
29      public Applicant() {
30          log.info(super.toString() + ", ctor()");     }
31      
32      public long getId() { return id; }
33  
34      public Borrower getBorrower() { return borrower; }
35      public void setBorrower(Borrower borrower) {
36          this.borrower = borrower;
37      }
38  
39      public Person getIdentity() { return identity; }
40      public void setIdentity(Person identity) {
41          this.identity = identity;
42      }
43  
44      public String toString() {
45          return super.toString() +
46              "id=" + id + 
47              ", identity=" + ((identity==null) ? "null" : identity.getId()) +
48              ", borrower=" + ((borrower==null) ? "null" : borrower.getId());
49      }
50  
51  }