View Javadoc
1   package ejava.jpa.examples.query;
2   
3   import java.math.BigDecimal;
4   
5   import java.text.DateFormat;
6   import java.text.NumberFormat;
7   import java.text.SimpleDateFormat;
8   import java.util.ArrayList;
9   import java.util.Date;
10  import java.util.List;
11  import java.util.Locale;
12  
13  import javax.persistence.*;
14  
15  @Entity
16  @Table(name="JPAQL_SALE")
17  public class Sale {
18      @Id @GeneratedValue 
19      @Column(name="SALE_ID")
20      private long id;
21  
22      @Temporal(TemporalType.TIMESTAMP)
23      private Date date;
24  
25      @Column(precision=5, scale=2)
26      private BigDecimal amount = new BigDecimal(0);
27      
28      /** This property has been purposely modeled as an ID and not a
29       * relationship to show how JPA queries can still functionally associate
30       * information without an explicit foreign key
31       */
32      @Column(name="BUYER_ID", nullable=false)
33      private long buyerId;
34  
35      @ManyToOne(optional=false)
36      @JoinColumn(name="SALE_STORE")
37      private Store store;
38  
39      @ManyToMany
40      @JoinTable(name="JPAQL_SALE_CLERK_LINK",
41          joinColumns={@JoinColumn(name="SALE_ID")},
42          inverseJoinColumns={@JoinColumn(name="CLERK_ID")}
43          )
44      private List<Clerk> clerks = new ArrayList<Clerk>();
45      
46      public long getId() { return id; }
47      
48      public List<Clerk> getClerks() { return clerks; }
49      public Sale setClerks(List<Clerk> clerks) {
50          this.clerks = clerks;
51          return this;
52      }
53      public Sale addClerk(Clerk...clerk) {
54      	if (clerk!=null) {
55      		for (Clerk c : clerk) {
56      			if (c != null) { clerks.add(c); }
57      		}
58      	}
59      	return this;
60      }
61      
62      public Store getStore() { return store; }
63      public Sale setStore(Store store) {
64          this.store = store;
65          return this;
66      }    
67  
68      public BigDecimal getAmount() { return amount; }
69      public Sale setAmount(BigDecimal amount) {
70          this.amount = amount;
71          return this;
72      }
73      
74      public long getBuyerId() { return buyerId; }
75      public Sale setBuyerId(long buyerId) {
76          this.buyerId = buyerId;
77          return this;
78      }
79      
80      public Date getDate() { return date; }
81      public Sale setDate(Date date) {
82          this.date = date;
83          return this;
84      }
85      
86      public String toString() {
87      	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
88      	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
89          StringBuilder text = new StringBuilder();
90          text.append("date=" + (date==null ? null : df.format(date)));
91          text.append(", amount=" + nf.format(amount));
92          text.append(", buyer=" + buyerId);
93          text.append(", clerks(" + clerks.size() + ")={");
94          for(Clerk c : clerks) {
95              text.append(c.getId() + ", ");
96          }            
97          text.append("}");
98          return text.toString();
99      }
100 }