1 package org.hibernate.ce.auction.model; 2 3 import javax.persistence.*; 4 import java.io.Serializable ; 5 import java.util.*; 6 7 24 @Entity(access = AccessType.FIELD) 25 @Table(uniqueConstraints = 26 {@UniqueConstraint(columnNames = 27 {"CAT_NAME", "PARENT_CAT_ID"} ) 28 } 29 ) 30 public class Category implements Serializable , Comparable { 31 32 @Id(generate = GeneratorType.AUTO) 33 @Column(name = "CAT_ID") 34 private Long id = null; 35 36 @Version 37 private int version = 0; 38 39 @Column(name = "CAT_NAME", length = 255, nullable = false) 40 private String name; 41 42 @ManyToOne 43 @JoinColumn(name = "PARENT_CAT_ID", nullable = true) 44 private Category parentCategory; 45 46 @OneToMany(cascade = CascadeType.ALL, mappedBy = "parentCategory") 47 @org.hibernate.annotations.BatchSize(size = 10) 48 @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) 49 private Set<Category> childCategories = new HashSet<Category>(); 50 51 @OneToMany(cascade = CascadeType.ALL, mappedBy = "category") 52 @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) 53 private Set<CategorizedItem> categorizedItems = new HashSet<CategorizedItem>(); 54 55 @Column( nullable = false, updatable = false) 56 private Date created = new Date(); 57 58 61 Category() {} 62 63 66 public Category(String name, Category parentCategory, Set<Category> childCategories, Set<CategorizedItem> categorizedItems) { 67 this.name = name; 68 this.parentCategory = parentCategory; 69 this.childCategories = childCategories; 70 this.categorizedItems = categorizedItems; 71 } 72 73 76 public Category(String name) { 77 this.name = name; 78 } 79 80 82 public Long getId() { return id; } 83 public int getVersion() { return version; } 84 85 public String getName() { return name; } 86 public void setName(String name) { this.name = name; } 87 88 public Category getParentCategory() { return parentCategory; } 89 public void setParentCategory(Category parentCategory) { this.parentCategory = parentCategory; } 90 91 public Set getChildCategories() { return childCategories; } 92 public void addChildCategory(Category category) { 93 if (category == null) 94 throw new IllegalArgumentException ("Can't add a null Category as child."); 95 if (category.getParentCategory() != null) 97 category.getParentCategory().getChildCategories() 98 .remove(category); 99 category.setParentCategory(this); 101 this.getChildCategories().add(category); 103 } 104 105 public Set getCategorizedItems() { return categorizedItems; } 106 public void addCategorizedItem(CategorizedItem catItem) { 107 if (catItem == null) 108 throw new IllegalArgumentException ("Can't add a null CategorizedItem."); 109 this.getCategorizedItems().add(catItem); 110 } 111 112 public Date getCreated() { return created; } 113 114 116 public boolean equals(Object o) { 117 if (this == o) return true; 118 if (!(o instanceof Category)) return false; 119 120 final Category category = (Category) o; 121 122 if (created != null ? !created.equals(category.created) : category.created != null) return false; 123 if (name != null ? !name.equals(category.name) : category.name != null) return false; 124 125 return true; 126 } 127 128 public int hashCode() { 129 int result; 130 result = (name != null ? name.hashCode() : 0); 131 result = 29 * result + (created != null ? created.hashCode() : 0); 132 return result; 133 } 134 135 public String toString() { 136 return "Category ('" + getId() + "'), " + 137 "Name: '" + getName() + "'"; 138 } 139 140 public int compareTo(Object o) { 141 if (o instanceof Category) { 142 return this.getName().compareTo( ((Category)o).getName() ); 143 } 144 return 0; 145 } 146 147 149 } 150 | Popular Tags |