View Javadoc
1   package ejava.examples.orm.core.mapped;
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. All fields will be mapped by the 
12   * containing class.
13   */
14  @Embeddable
15  public class MakeModelPK implements Serializable {
16      private static final long serialVersionUID = 1L;
17      private String make;
18      private String model;
19      
20      public MakeModelPK() {}
21      public MakeModelPK(String make, String model) {
22          this.make = make;
23          this.model = model;
24      }
25      
26      public String getMake() { return make; }
27      public String getModel() { return model; }
28  
29      public int hashCode() { return make.hashCode() + model.hashCode(); }
30      public boolean equals(Object obj) {
31          try {
32              if (this == obj) return true;
33              return make.equals(((MakeModelPK)obj).getMake()) &&
34                     model.equals(((MakeModelPK)obj).getModel());
35              
36          } catch (Throwable ignored) { //catch NP & Cast Exceptions 
37              return false;
38          }
39      }
40      
41      public String toString() {
42          return super.toString() +
43              ", make=" + make +
44              ", model=" + model;
45      }
46  }