View Javadoc
1   package ejava.examples.orm.core.annotated;
2   
3   import java.io.Serializable;
4   
5   import javax.persistence.*;
6   
7   /**
8    * This class provides an example of a primary key class that can be 
9    * embedded into the referenced class. The containing class will simply
10   * use an instance of this class rather than having separate fields that
11   * match the fields of this class.
12   */
13  @Embeddable
14  public class NapsackPK implements Serializable {
15      private static final long serialVersionUID = 1L;
16  
17      @Column(name="NAPSACK_MAKE") //maps field to column of containing class
18      private String make;
19      @Column(name="NAPSACK_MODEL")//maps field to column of containing class
20      private String model;
21      
22      public NapsackPK() {}
23      public NapsackPK(String make, String model) {
24          this.make = make;
25          this.model = model;
26      }
27      
28      public String getMake() { return make; }
29      public void setMake(String make) {
30  		this.make = make;
31  	}
32      public String getModel() { return model; }
33      public void setModel(String model) {
34  		this.model = model;
35  	}
36  
37      public int hashCode() { return make.hashCode() + model.hashCode(); }
38      public boolean equals(Object obj) {
39          try {
40              if (this == obj) return true;
41              return make.equals(((NapsackPK)obj).getMake()) &&
42                     model.equals(((NapsackPK)obj).getModel());
43              
44          } catch (Throwable ignored) { //catch NP & Cast Exceptions 
45              return false;
46          }
47      }
48      
49      public String toString() {
50          return new StringBuilder()
51              .append(super.toString())	       
52              .append(", make=").append(make)
53              .append(", model=").append(model)
54              .toString();
55      }
56  }