diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-11-22 15:07:27 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-11-23 15:57:23 +0100 |
| commit | df8c8d8eaa3fd74b2f6a76341611555ee6d8834d (patch) | |
| tree | 40e93e3b3674f32d4870486eb9f5870d9fe45b2b /src/jpa | |
| download | JCatalog-df8c8d8eaa3fd74b2f6a76341611555ee6d8834d.tar.gz JCatalog-df8c8d8eaa3fd74b2f6a76341611555ee6d8834d.zip | |
Add initial files
Diffstat (limited to 'src/jpa')
| -rw-r--r-- | src/jpa/Category.java | 103 | ||||
| -rw-r--r-- | src/jpa/JPATest.java | 62 | ||||
| -rw-r--r-- | src/jpa/Product.java | 113 | ||||
| -rw-r--r-- | src/jpa/User.java | 36 |
4 files changed, 314 insertions, 0 deletions
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<Product> 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 <code>getProductCollection()</code> returns a + * Collection object that is incompatible with JSF. + * + * @return collection of product objects. + */ + public Collection<Product> getProducts() + { + Collection<Product> c = new ArrayList<Product>(); + + for (Iterator iter = getProductCollection().iterator(); iter.hasNext();) + { + Product product = (Product) iter.next(); + c.add(product); +// System.out.println(product.getName()); + } + + return c; + } + + public Set<Product> getProductCollection() + { + return this.productCollection; + } + + public void setProductCollection(Set<Product> productCollection) + { + this.productCollection = productCollection; + } + + public void addProduct(Product product) + { + Set<Product> products = getProductCollection(); + if (!products.contains(product)) + { + products.add(product); + product.addCategory(this); + } + } + +} diff --git a/src/jpa/JPATest.java b/src/jpa/JPATest.java new file mode 100644 index 0000000..5a4749e --- /dev/null +++ b/src/jpa/JPATest.java @@ -0,0 +1,62 @@ +package jpa; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +public class JPATest +{ + /** + * @param args + */ + public static void main(String[] args) + { + EntityManagerFactory factory = Persistence + .createEntityManagerFactory("catalog"); + EntityManager manager = factory.createEntityManager(); + EntityTransaction tx = manager.getTransaction(); + tx.begin(); + try + { +// Product product = manager.find(Product.class, "7"); +// Category newCategory = manager.find(Category.class, 4); +// product.addCategory(newCategory); +// manager.persist(product); +// Collection<Category> categories = product.getCategoryCollection(); +// for (Category category : categories) +// { +// System.out.println(category.getName()); +// } + Product product = new Product(); + product.setDescription("Noch ein Testprodukt"); + product.setName("Produkt"); + manager.persist(product); +// User user = new User(); +// user.setUsername("bestertester2"); +// System.out.println(DigestUtils.md5("masterkey")); +// user.setPassword(DigestUtils.md5("masterkey")); +// manager.persist(user); + + tx.commit(); + } + catch (Exception ex) + { + ex.printStackTrace(System.err); + tx.rollback(); + } + +// CategoryManager cm = new CategoryManager(); +// Collection<Category> categories = cm.getCategories(); +// for (Category category : categories) +// { +// System.out.println(category.getName()); +// Set<Product> products = category.getProductCollection(); +// for (Product product : products) +// { +// System.out.println("\t" + product.getName()); +// } +// +// } + } +} diff --git a/src/jpa/Product.java b/src/jpa/Product.java new file mode 100644 index 0000000..dc0b3f8 --- /dev/null +++ b/src/jpa/Product.java @@ -0,0 +1,113 @@ +package jpa; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.Collection; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; + +@Entity +public class Product implements Serializable { + @Id + @GeneratedValue(strategy=GenerationType.SEQUENCE) + private String id; + + private String description; + + private BigDecimal price; + + private String name; + + private BigDecimal width; + + private BigDecimal height; + + @ManyToMany + @JoinTable( + joinColumns=@JoinColumn(name="product_id"), + inverseJoinColumns=@JoinColumn(name="category_id")) + private Set<Category> categoryCollection; + + private static final long serialVersionUID = 1L; + + public Product() { + super(); + } + + public String getId() { + return this.id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return this.price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public BigDecimal getWidth() { + return this.width; + } + + public void setWidth(BigDecimal width) { + this.width = width; + } + + public BigDecimal getHeight() { + return this.height; + } + + public void setHeight(BigDecimal height) { + this.height = height; + } + + public Collection getCategories() { + return this.getCategoryCollection(); + } + + public Set<Category> getCategoryCollection() { + return this.categoryCollection; + } + + public void setCategoryCollection(Set<Category> categoryCollection) { + this.categoryCollection = categoryCollection; + } + + public void addCategory(Category category) + { + Set<Category> categories = getCategoryCollection(); + if (!categories.contains(category)) + { + categories.add(category); + category.addProduct(this); + } + } +} diff --git a/src/jpa/User.java b/src/jpa/User.java new file mode 100644 index 0000000..32d2af8 --- /dev/null +++ b/src/jpa/User.java @@ -0,0 +1,36 @@ +package jpa; + +import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class User implements Serializable { + @Id + private String username; + + private String password; + + private static final long serialVersionUID = 1L; + + public User() { + super(); + } + + public String getUsername() { + return this.username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + +} |
