From df8c8d8eaa3fd74b2f6a76341611555ee6d8834d Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sun, 22 Nov 2015 15:07:27 +0100 Subject: Add initial files --- src/jpa/Category.java | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/jpa/Category.java (limited to 'src/jpa/Category.java') diff --git a/src/jpa/Category.java b/src/jpa/Category.java new file mode 100644 index 0000000..c29c1f7 --- /dev/null +++ b/src/jpa/Category.java @@ -0,0 +1,103 @@ +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); + } + } + +} -- cgit v1.2.3-70-g09d2