package jpa; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Set; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Category implements Serializable { @Id private int id; private String name; private String description; @ManyToMany(mappedBy = "categoryCollection") private Set productCollection; private static final long serialVersionUID = 1L; public Category() { super(); } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } /** * The method getProductCollection() returns a * Collection object that is incompatible with JSF. * * @return collection of product objects. */ public Collection getProducts() { Collection c = new ArrayList(); for (Iterator iter = getProductCollection().iterator(); iter.hasNext();) { Product product = (Product) iter.next(); c.add(product); // System.out.println(product.getName()); } return c; } public Set getProductCollection() { return this.productCollection; } public void setProductCollection(Set productCollection) { this.productCollection = productCollection; } public void addProduct(Product product) { Set products = getProductCollection(); if (!products.contains(product)) { products.add(product); product.addCategory(this); } } }