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.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
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 Logger log = LoggerFactory.getLogger(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              cascade= { CascadeType.REFRESH }) //recheck of borrow still target of foreign key when refresh borrower
44      private Applicant application;
45      
46      @OneToMany(mappedBy="borrower", //this relationship is owned by Checkout
47              fetch=FetchType.LAZY)   //try to limit what we get back
48      private Collection<Checkout> checkouts = new ArrayList<Checkout>();
49      
50      protected Borrower() { log.info("{}, ctor()", myInstance()); }
51      public Borrower(Person identity) {
52          log.info("{}, ctor():", myInstance(), identity);
53          this.id = identity.getId();
54          this.identity = identity;
55      }
56  
57      public long getId() { return id; }
58  
59      public Date getEndDate() { return endDate; }
60      public void setEndDate(Date end) {
61          this.endDate = end;
62      }
63  
64      public Date getStartDate() { return startDate; }
65      public void setStartDate(Date start) {
66          this.startDate = start;
67      }
68      
69      /** This method returns the concatenated first/last names of the 
70       * Borrower's person. Note that we need to declare this field Transient
71       * so that the provider doesn't try to map this to a column in the 
72       * Borrower table as well as look for a setName.
73       */
74      //@Transient - not needed when using FIELD mapping versus PROPERTY mapping
75      public String getName() {
76          return identity.getFirstName() + " " + identity.getLastName();
77      }
78  
79      public Applicant getApplication() { return application; }
80      public void setApplication(Applicant application) {
81          this.application = application;
82      }
83  
84      public Collection<Checkout> getCheckouts() {
85          //don't let them directly touch our collection
86          return new ArrayList<Checkout>(checkouts);
87      }
88      public void addCheckout(Checkout checkout) {
89          this.checkouts.add(checkout);
90      }
91      public void removeCheckout(Checkout checkout) {
92          this.checkouts.remove(checkout);
93      }
94  
95      private String myInstance() {
96          String s=super.toString();
97          s = s.substring(s.lastIndexOf('.')+1);
98          return s;
99      }
100     
101     public String toString() {
102         StringBuilder text = new StringBuilder(
103             getClass().getSimpleName() +
104             ", id=" + id +
105             ", startDate=" + startDate +
106             ", endDate=" + endDate +
107             ", identity=" + identity +
108             ", applicant=" + application +
109             ", checkouts={");
110         for(Iterator<Checkout> itr=checkouts.iterator(); itr.hasNext();) {
111             text.append(itr.next().getId());
112             if (itr.hasNext()) { text.append(","); }
113         }
114         text.append("}");
115         return text.toString();
116     }
117 }