View Javadoc
1   package ejava.examples.orm.rel.annotated;
2   
3   import javax.persistence.*;
4   
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
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 Logger log = LoggerFactory.getLogger(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("{}, ctor()", myInstance());     }
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      private String myInstance() {
45          String s=super.toString();
46          s = s.substring(s.lastIndexOf('.')+1);
47          return s;
48      }
49      
50      public String toString() {
51          return myInstance() +
52              "id=" + id + 
53              ", identity=" + ((identity==null) ? "null" : identity.getId()) +
54              ", borrower=" + ((borrower==null) ? "null" : borrower.getId());
55      }
56  
57  }