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
15
16
17
18
19
20
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
37
38 private Person identity;
39
40 @OneToOne(fetch=FetchType.LAZY,
41 optional=true,
42 mappedBy="borrower")
43 private Applicant application;
44
45 @OneToMany(mappedBy="borrower",
46 fetch=FetchType.LAZY)
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
69
70
71
72
73
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
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 }