Sale.java

  1. package ejava.jpa.examples.query;

  2. import java.math.BigDecimal;

  3. import java.text.DateFormat;
  4. import java.text.NumberFormat;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Locale;

  10. import javax.persistence.*;

  11. @Entity
  12. @Table(name="JPAQL_SALE")
  13. public class Sale {
  14.     @Id @GeneratedValue
  15.     @Column(name="SALE_ID")
  16.     private long id;

  17.     @Temporal(TemporalType.TIMESTAMP)
  18.     private Date date;

  19.     @Column(precision=5, scale=2)
  20.     private BigDecimal amount = new BigDecimal(0);
  21.    
  22.     /** This property has been purposely modeled as an ID and not a
  23.      * relationship to show how JPA queries can still functionally associate
  24.      * information without an explicit foreign key
  25.      */
  26.     @Column(name="BUYER_ID", nullable=false)
  27.     private long buyerId;

  28.     @ManyToOne(optional=false)
  29.     @JoinColumn(name="SALE_STORE")
  30.     private Store store;

  31.     @ManyToMany
  32.     @JoinTable(name="JPAQL_SALE_CLERK_LINK",
  33.         joinColumns={@JoinColumn(name="SALE_ID")},
  34.         inverseJoinColumns={@JoinColumn(name="CLERK_ID")}
  35.         )
  36.     private List<Clerk> clerks = new ArrayList<Clerk>();
  37.    
  38.     public long getId() { return id; }
  39.    
  40.     public List<Clerk> getClerks() { return clerks; }
  41.     public Sale setClerks(List<Clerk> clerks) {
  42.         this.clerks = clerks;
  43.         return this;
  44.     }
  45.     public Sale addClerk(Clerk...clerk) {
  46.         if (clerk!=null) {
  47.             for (Clerk c : clerk) {
  48.                 if (c != null) { clerks.add(c); }
  49.             }
  50.         }
  51.         return this;
  52.     }
  53.    
  54.     public Store getStore() { return store; }
  55.     public Sale setStore(Store store) {
  56.         this.store = store;
  57.         return this;
  58.     }    

  59.     public BigDecimal getAmount() { return amount; }
  60.     public Sale setAmount(BigDecimal amount) {
  61.         this.amount = amount;
  62.         return this;
  63.     }
  64.    
  65.     public long getBuyerId() { return buyerId; }
  66.     public Sale setBuyerId(long buyerId) {
  67.         this.buyerId = buyerId;
  68.         return this;
  69.     }
  70.    
  71.     public Date getDate() { return date; }
  72.     public Sale setDate(Date date) {
  73.         this.date = date;
  74.         return this;
  75.     }
  76.    
  77.     public String toString() {
  78.         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  79.         NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
  80.         StringBuilder text = new StringBuilder();
  81.         text.append("date=" + (date==null ? null : df.format(date)));
  82.         text.append(", amount=" + nf.format(amount));
  83.         text.append(", buyer=" + buyerId);
  84.         text.append(", clerks(" + clerks.size() + ")={");
  85.         for(Clerk c : clerks) {
  86.             text.append(c.getId() + ", ");
  87.         }            
  88.         text.append("}");
  89.         return text.toString();
  90.     }
  91. }