View Javadoc
1   package ejava.examples.orm.rel.annotated;
2   
3   import java.util.Date;
4   import java.util.Collection;
5   import java.util.ArrayList;
6   import java.util.Iterator;
7   
8   import javax.persistence.*;
9   
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  
13  /**
14   * This class forms a uni-directional, one-to-one relationship with the
15   * Person class, joined by common primary keys. There is not additional
16   * column necessary to store the foreign key.
17   * 
18   * Note too that this object fully encapsulates the relationship by never
19   * handing over the Person; only using it to derive values. All concenience
20   * getters are declared Transient.
21   */
22  @Entity @Table(name="ORMREL_BORROWER")
23  public class Borrower  {
24      private static Log log = LogFactory.getLog(Borrower.class);
25      @Id @Column(name="BORROWER_ID")
26      private long id;
27      @Temporal(value=TemporalType.DATE)
28      private Date startDate;
29      @Temporal(value=TemporalType.DATE)
30      private Date endDate;
31      
32      @OneToOne(fetch=FetchType.LAZY, optional=false, 
33              cascade={CascadeType.PERSIST, 
34                       CascadeType.REFRESH,
35                       CascadeType.MERGE})
36      @PrimaryKeyJoinColumn  //the two tables will be joined by PKs
37      //@MapsId
38      private Person identity;
39      
40      @OneToOne(fetch=FetchType.LAZY, 
41              optional=true,       //lets make this optional for demo 
42              mappedBy="borrower") //the other side owns foreign key column
43      private Applicant application;
44      
45      @OneToMany(mappedBy="borrower", //this relationship is owned by Checkout
46              fetch=FetchType.LAZY)   //try to limit what we get back
47      private Collection<Checkout> checkouts = new ArrayList<Checkout>();
48      
49      protected Borrower() { log.info(super.toString() + ", ctor()"); }
50      public Borrower(Person identity) {
51          log.info(super.toString() + ", ctor():" + identity);
52          this.id = identity.getId();
53          this.identity = identity;
54      }
55  
56      public long getId() { return id; }
57  
58      public Date getEndDate() { return endDate; }
59      public void setEndDate(Date end) {
60          this.endDate = end;
61      }
62  
63      public Date getStartDate() { return startDate; }
64      public void setStartDate(Date start) {
65          this.startDate = start;
66      }
67      
68      /** This method returns the concatenated first/last names of the 
69       * Borrower's person. Note that we need to declare this field Transient
70       * so that the provider doesn't try to map this to a column in the 
71       * Borrower table as well as look for a setName.
72       */
73      //@Transient - not needed when using FIELD mapping versus PROPERTY mapping
74      public String getName() {
75          return identity.getFirstName() + " " + identity.getLastName();
76      }
77  
78      public Applicant getApplication() { return application; }
79      public void setApplication(Applicant application) {
80          this.application = application;
81      }
82  
83      public Collection<Checkout> getCheckouts() {
84          //don't let them directly touch our collection
85          return new ArrayList<Checkout>(checkouts);
86      }
87      public void addCheckout(Checkout checkout) {
88          this.checkouts.add(checkout);
89      }
90      public void removeCheckout(Checkout checkout) {
91          this.checkouts.remove(checkout);
92      }
93      
94      public String toString() {
95          StringBuilder text = new StringBuilder(
96              getClass().getName() +
97              ", id=" + id +
98              ", startDate=" + startDate +
99              ", endDate=" + endDate +
100             ", identity=" + identity +
101             ", applicant=" + application +
102             ", checkouts={");
103         for(Iterator<Checkout> itr=checkouts.iterator(); itr.hasNext();) {
104             text.append(itr.next().getId());
105             if (itr.hasNext()) { text.append(","); }
106         }
107         text.append("}");
108         return text.toString();
109     }
110 }