KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.ce.auction.model;
2
3 import javax.persistence.*;
4 import java.io.Serializable JavaDoc;
5 import java.util.Date JavaDoc;
6
7 /**
8  * A single item in a single category, with additional information.
9  * <p>
10  * This is really a very special mapping. The CategorizedItem class
11  * represents an association table. The ER model for this is really
12  * a many-to-many association, but instead of two entities and two
13  * collections, we mapped this as two one-to-many associations between
14  * three entities. One of the motivation for this are the additional
15  * attributes on the association table (not only two FKs): username
16  * and creation date.
17  *
18  * @see org.hibernate.ce.auction.model.Category
19  * @see org.hibernate.ce.auction.model.Item
20  * @author Christian Bauer <christian@hibernate.org>
21  */

22 @Entity(access = AccessType.FIELD)
23 @Table(name = "CATEGORIZED_ITEM")
24 public class CategorizedItem implements Serializable JavaDoc, Comparable JavaDoc {
25
26     @Embeddable(access = AccessType.FIELD)
27     public static class Id implements Serializable JavaDoc {
28
29         @Column(name = "CAT_ID")
30         private Long JavaDoc categoryId;
31         @Column(name = "ITEM_ID")
32         private Long JavaDoc itemId;
33
34         public Id() {}
35
36         public Id(Long JavaDoc categoryId, Long JavaDoc itemId) {
37             this.categoryId = categoryId;
38             this.itemId = itemId;
39         }
40
41         public boolean equals(Object JavaDoc o) {
42             if (o instanceof Id) {
43                 Id that = (Id)o;
44                 return this.categoryId.equals(that.categoryId) &&
45                        this.itemId.equals(that.itemId);
46             } else {
47                 return false;
48             }
49         }
50
51         public int hashCode() {
52             return categoryId.hashCode() + itemId.hashCode();
53         }
54     }
55
56     @EmbeddedId
57     private Id id = new Id();
58
59     @Column(name = "ADDED_BY_USER")
60     private String JavaDoc username; // This could also be an association to User
61

62     @Column(name = "ADDED_ON")
63     private Date JavaDoc dateAdded = new Date JavaDoc();
64
65     @ManyToOne
66     @JoinColumn(name="ITEM_ID", insertable = false, updatable = false)
67     private Item item;
68
69     @ManyToOne
70     @JoinColumn(name="CAT_ID", insertable = false, updatable = false)
71     private Category category;
72
73     /**
74      * No-arg constructor for JavaBean tools.
75      */

76     CategorizedItem() {}
77
78     /**
79      * Full constructor;
80      */

81     public CategorizedItem(String JavaDoc username, Category category, Item item) {
82         this.username = username;
83
84         this.category = category;
85         this.item = item;
86
87         // Set key values
88
this.id.categoryId = category.getId();
89         this.id.itemId = item.getId();
90
91         // Guarantee referential integrity
92
category.getCategorizedItems().add(this);
93         item.getCategorizedItems().add(this);
94     }
95
96     // ********************** Accessor Methods ********************** //
97

98     public Id getId() { return id; }
99
100     public String JavaDoc getUsername() { return username; }
101     public Date JavaDoc getDateAdded() { return dateAdded; }
102
103     public Category getCategory() { return category; }
104     public Item getItem() { return item; }
105
106     // ********************** Common Methods ********************** //
107

108     public int compareTo(Object JavaDoc o) {
109         // CategorizedItems are sorted by date
110
if (o instanceof CategorizedItem)
111             return getDateAdded().compareTo( ((CategorizedItem)o).getDateAdded() );
112         return 0;
113     }
114
115     public String JavaDoc toString() {
116         return "Added by: '" + getUsername() + "', " +
117                 "On Date: '" + getDateAdded();
118     }
119
120     // ********************** Business Methods ********************** //
121

122 }
Popular Tags