View Javadoc
1   package info.ejava.examples.ejb.tx.bo;
2   
3   import java.io.Serializable;
4   
5   import javax.persistence.Column;
6   import javax.persistence.Entity;
7   import javax.persistence.GeneratedValue;
8   import javax.persistence.Id;
9   import javax.persistence.NamedNativeQueries;
10  import javax.persistence.NamedNativeQuery;
11  import javax.persistence.NamedQueries;
12  import javax.persistence.NamedQuery;
13  import javax.persistence.Table;
14  
15  @SuppressWarnings("serial")
16  @Entity
17  @Table(name="EJBTX_PRODUCT") 
18  @NamedQueries({
19      @NamedQuery(name="EJBTxProduct.addQuantity", 
20          query="update Product p "
21                  + "set p.quantity=p.quantity + :quantity "
22                  + "where p.id=:productId"),
23      @NamedQuery(name="EJBTxProduct.getQuantity", 
24          query="select p.quantity from Product p "
25                  + "where p.id = :productId"),                
26      @NamedQuery(name="EJBTxProduct.getRemainingQuantity", 
27          query="select p.quantity - count(s) from Product p, Shipment s "
28                  + "where p.id = :productId and s.productId = :productId"),
29      @NamedQuery(name="EJBTxProduct.getCount", 
30          query="select count(p) from Product p "
31                  + "where p.id = :productId")
32  })
33  @NamedNativeQueries({
34      @NamedNativeQuery(name="EJBTxProduct.getProduct", 
35          query="select PRODUCT_ID, PRODUCT_NAME, QUANTITY from EJBTX_PRODUCT where PRODUCT_ID=:productId")
36  })
37  public class Product implements Serializable {
38      @Id @GeneratedValue
39      @Column(name="PRODUCT_ID")
40      private int id;
41  
42      @Column(name="PRODUCT_NAME", length=16, nullable=false)
43      private String name;
44      
45      @Column(name="QUANTITY")
46      private int quantity;
47      
48      public Product() {}
49      public Product(int id) { this.id = id;}
50      public Product(String name, int quantity) {
51          this.name=name;
52          this.quantity=quantity;
53      }
54      public Product(Product p) {
55          this.id = p.id;
56          this.name = p.name;
57          this.quantity = p.quantity;
58      }
59  
60      public int getId() { return id; }
61      public void setId(int id) {
62          this.id = id;
63      }
64  
65      public String getName() { return name; }
66      public void setName(String name) {
67          this.name = name;
68      }
69  
70      public int getQuantity() { return quantity; }
71      public void setQuantity(int quantity) {
72          this.quantity = quantity;
73      }
74      
75      @Override
76      public String toString() {
77          StringBuilder builder = new StringBuilder();
78          builder.append("Product [id=").append(id)
79              .append(", name=").append(name)
80              .append(", quanity=").append(quantity).append("]");
81          return builder.toString();
82      }
83  }