View Javadoc
1   package myorg.relex.collection;
2   
3   
4   import javax.persistence.*;
5   
6   /**
7    * This class is provides an example of an entity that implements hashCode/equals 
8    * using its database assigned primary key if it exists and defaults to the 
9    * java.lang.Object definition if not yet assigned.  Note that this technique causes
10   * a change in hashCode/equals after the persist() takes place -- invalidating anything
11   * previously cached for the identity.
12   */
13  @Entity
14  @Table(name="RELATIONEX_SHIP")
15  public class ShipBySwitch extends Ship {
16  	@Override
17  	public int peekHashCode() {
18  		return id==0 ? super.objectHashCode() : id;
19  	}
20  
21  	@Override
22  	public boolean equals(Object obj) {
23  		try {
24              if (this == obj) { return logEquals(obj, true); }
25  			boolean equals = (id==0) ? super.equals(obj) :
26  				id==((ShipBySwitch)obj).id;
27  			return logEquals(obj, equals);
28  		} catch (Exception ex) {
29  			return logEquals(obj, false);
30  		}
31  	}
32  }