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