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