View Javadoc
1   package ejava.examples.orm.core;
2   
3   import java.io.Serializable;
4   
5   /**
6    * This is an example of a class that can be used for a Java Persistence
7    * IdClass. Its requirements are that it have a default ctor() and it 
8    * correctly override hashCode() and equals(). Not annotations or orm.xml
9    * mappings are required for this specific class. All annotations and orm.xml 
10   * entries are supplied by the classes this PK class identifies.
11   */
12  public class MowerPK implements Serializable {
13      private static final long serialVersionUID = 1L;
14      private String make;
15      private String model;
16      
17      public MowerPK() {}
18      public MowerPK(String make, String model) {
19          this.make = make;
20          this.model = model;
21      }
22  
23      public String getMake() { return make; }
24      public String getModel() { return model; }
25      
26      @Override
27      public int hashCode() { return make.hashCode() + model.hashCode(); }
28      @Override
29      public boolean equals(Object obj) {
30          try {
31              if (this == obj) return true;
32              return make.equals(((MowerPK)obj).getMake()) &&
33                     model.equals(((MowerPK)obj).getModel());
34              
35          } catch (Throwable ignored) { //catch NP & Cast Exceptions 
36              return false;
37          }
38      }
39      
40      @Override
41      public String toString() {
42          return super.toString() +
43              ", make=" + make +
44              ", model=" + model;
45      }
46  }