1 package org.hibernate.ce.auction.model; 2 3 import javax.persistence.*; 4 import java.io.Serializable ; 5 import java.util.Date ; 6 7 22 @Entity(access = AccessType.FIELD) 23 @Table(name = "CATEGORIZED_ITEM") 24 public class CategorizedItem implements Serializable , Comparable { 25 26 @Embeddable(access = AccessType.FIELD) 27 public static class Id implements Serializable { 28 29 @Column(name = "CAT_ID") 30 private Long categoryId; 31 @Column(name = "ITEM_ID") 32 private Long itemId; 33 34 public Id() {} 35 36 public Id(Long categoryId, Long itemId) { 37 this.categoryId = categoryId; 38 this.itemId = itemId; 39 } 40 41 public boolean equals(Object 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 username; 62 @Column(name = "ADDED_ON") 63 private Date dateAdded = new Date (); 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 76 CategorizedItem() {} 77 78 81 public CategorizedItem(String username, Category category, Item item) { 82 this.username = username; 83 84 this.category = category; 85 this.item = item; 86 87 this.id.categoryId = category.getId(); 89 this.id.itemId = item.getId(); 90 91 category.getCategorizedItems().add(this); 93 item.getCategorizedItems().add(this); 94 } 95 96 98 public Id getId() { return id; } 99 100 public String getUsername() { return username; } 101 public Date getDateAdded() { return dateAdded; } 102 103 public Category getCategory() { return category; } 104 public Item getItem() { return item; } 105 106 108 public int compareTo(Object o) { 109 if (o instanceof CategorizedItem) 111 return getDateAdded().compareTo( ((CategorizedItem)o).getDateAdded() ); 112 return 0; 113 } 114 115 public String toString() { 116 return "Added by: '" + getUsername() + "', " + 117 "On Date: '" + getDateAdded(); 118 } 119 120 122 } | Popular Tags |