View Javadoc
1   package ejava.examples.orm.rel.annotated;
2   
3   import java.util.Date;
4   
5   import javax.persistence.*;
6   
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  @Entity @Table(name="ORMREL_CHECKOUT")
11  public class Checkout {
12      private static Logger log = LoggerFactory.getLogger(Checkout.class); 
13      private static long CHECKOUT_DAYS = 1000 * 60 * 60 * 24 * 14;
14  
15      @Id @GeneratedValue @Column(name="CHECKOUT_ID")
16      private long id;
17  
18      @Temporal(value=TemporalType.DATE)
19      private Date outDate;
20  
21      @Temporal(value=TemporalType.DATE)
22      private Date returnDate;
23      @ManyToOne(optional=false, fetch=FetchType.LAZY)
24      @JoinColumn(name="CHECKOUT_BID")
25      private Borrower borrower;
26      
27      protected Checkout() {  //this is needed by persistence layer
28          log.debug(super.toString() + ": ctor()");
29      }  
30      public Checkout(Date outDate) {
31          log.debug(super.toString() + ": ctor() outDate=" + outDate);
32          this.outDate = outDate;
33      }
34      public boolean isOverdue() {
35          return (returnDate == null) ?
36              System.currentTimeMillis() - outDate.getTime() > CHECKOUT_DAYS : 
37              returnDate.getTime() - outDate.getTime()  > CHECKOUT_DAYS;
38      }
39      
40      public long getId() { return id; }
41  
42      public Borrower getBorrower() { return borrower; }
43      public void setBorrower(Borrower borrower) {
44          this.borrower = borrower;
45      }
46  
47      public Date getReturnDate() { return returnDate; }
48      public void setReturnDate(Date returnDate) {
49          this.returnDate = returnDate;
50      }
51  
52      private String myInstance() {
53          String s=super.toString();
54          s = s.substring(s.lastIndexOf('.')+1);
55          return s;
56      }
57      
58      public String toString() {
59          return myInstance() +
60              ", id=" + id +
61              ", outDate=" + outDate +
62              ", returnDate=" + returnDate +
63              ", isOverdue=" + isOverdue() +
64              ", borrower=" + borrower;
65      }
66  }