KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > model > Category


1 package org.hibernate.ce.auction.model;
2
3 import javax.persistence.*;
4 import java.io.Serializable JavaDoc;
5 import java.util.*;
6
7 /**
8  * The CaveatEmptor Category can have child categories and each has Items.
9  * <p>
10  * Categories can be nested, this is expressed as a bidirectional one-to-many
11  * relationship that references parent and child categories. Each Category
12  * can have many Items (and an Item can be in many categories). This is a
13  * true many-to-many relationship.
14  * <p>
15  * The optional class <tt>CategorizedItem</tt> can be used if additional
16  * information has to be kept about the link between a Category and an
17  * Item. The collection of <tt>items</tt> will then be mapped as a
18  * collection of dependent objects in the mapping for <tt>Category</tt>.
19  *
20  * @see Item
21  * @see CategorizedItem
22  * @author Christian Bauer <christian@hibernate.org>
23  */

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 JavaDoc, Comparable JavaDoc {
31
32     @Id(generate = GeneratorType.AUTO)
33     @Column(name = "CAT_ID")
34     private Long JavaDoc id = null;
35
36     @Version
37     private int version = 0;
38
39     @Column(name = "CAT_NAME", length = 255, nullable = false)
40     private String JavaDoc 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     /**
59      * No-arg constructor for JavaBean tools.
60      */

61     Category() {}
62
63     /**
64      * Full constructor.
65      */

66     public Category(String JavaDoc 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     /**
74      * Simple constructor.
75      */

76     public Category(String JavaDoc name) {
77         this.name = name;
78     }
79
80     // ********************** Accessor Methods ********************** //
81

82     public Long JavaDoc getId() { return id; }
83     public int getVersion() { return version; }
84
85     public String JavaDoc getName() { return name; }
86     public void setName(String JavaDoc 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 JavaDoc("Can't add a null Category as child.");
95         // Remove from old parent category
96
if (category.getParentCategory() != null)
97             category.getParentCategory().getChildCategories()
98                                          .remove(category);
99         // Set parent in child
100
category.setParentCategory(this);
101         // Set child in parent
102
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 JavaDoc("Can't add a null CategorizedItem.");
109         this.getCategorizedItems().add(catItem);
110     }
111
112     public Date getCreated() { return created; }
113
114     // ********************** Common Methods ********************** //
115

116     public boolean equals(Object JavaDoc 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 JavaDoc toString() {
136         return "Category ('" + getId() + "'), " +
137                 "Name: '" + getName() + "'";
138     }
139
140     public int compareTo(Object JavaDoc o) {
141         if (o instanceof Category) {
142             return this.getName().compareTo( ((Category)o).getName() );
143         }
144         return 0;
145     }
146
147     // ********************** Business Methods ********************** //
148

149 }
150
Popular Tags