View Javadoc
1   package ejava.examples.ejbwar.inventory.bo;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.persistence.Column;
7   import javax.persistence.Entity;
8   import javax.persistence.GeneratedValue;
9   import javax.persistence.GenerationType;
10  import javax.persistence.Id;
11  import javax.persistence.JoinTable;
12  import javax.persistence.ManyToMany;
13  import javax.persistence.NamedQueries;
14  import javax.persistence.NamedQuery;
15  import javax.persistence.Table;
16  import javax.persistence.Transient;
17  import javax.xml.bind.annotation.XmlAccessType;
18  import javax.xml.bind.annotation.XmlAccessorType;
19  import javax.xml.bind.annotation.XmlAttribute;
20  import javax.xml.bind.annotation.XmlElement;
21  import javax.xml.bind.annotation.XmlElementWrapper;
22  import javax.xml.bind.annotation.XmlRootElement;
23  import javax.xml.bind.annotation.XmlType;
24  
25  /**
26   * This class represents a product category which has a many-to-many 
27   * relationship with product. It has been mapped to the DB and XML.
28   */
29  @XmlRootElement(name="catageory", namespace=InventoryRepresentation.NAMESPACE)
30  @XmlType(name="Category", namespace=InventoryRepresentation.NAMESPACE)
31  @XmlAccessorType(XmlAccessType.PROPERTY)
32  
33  @Entity
34  @Table(name="JAXRSINV_CATEGORY")
35  @NamedQueries({
36  	@NamedQuery(name=Category.FIND_BY_NAME, 
37  		query="select c from Category c where name like :criteria"),
38  	@NamedQuery(name=Category.FIND_BY_PRODUCT, 
39  		query="select c from Category c " +
40  				"where :product member of c.products" )
41  })
42  public class Category extends InventoryRepresentation {
43  	private static final long serialVersionUID = 2367549577678745828L;
44  	public static final String FIND_BY_NAME = "Inventory.findCategoryByName";
45  	public static final String FIND_BY_PRODUCT = "Inventory.findCategoryByProduct";;
46  	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
47  	@Column(name="ID")
48  	private int id;
49  	
50  	@Column(name="NAME", unique=true)
51  	private String name;
52  	
53  	@Transient
54  	private Integer productCount;
55  	
56  	@ManyToMany
57  	@JoinTable(name="JAXRSINV_PROD_CAT")
58  	private List<Product> products=new ArrayList<Product>();
59  
60  	public Category() {}
61  	public Category(String name) {
62  		this.name=name;
63  	}
64  	
65  	@XmlAttribute(required=true)
66  	public int getId() { return id; }
67  	public void setId(int id) {
68  		this.id = id;
69  	}
70  	
71  	@XmlAttribute(required=true)
72  	public String getName() { return name; }
73  	public void setName(String name) {
74  		this.name = name;
75  	}
76  	
77  	@XmlElement(required=true)
78  	public int getProductCount() { return productCount!=null ? productCount :products.size();}
79  	public void setProductCount(int productCount) {
80  		this.productCount = productCount;
81  	}
82  	
83  	@XmlElementWrapper(name="products")
84  	@XmlElement(name="product")
85  	public List<Product> getProducts() { return products; }
86  	public void setProducts(List<Product> products) {
87  		this.products = products;
88  	}
89  }