View Javadoc
1   package myorg.relex.many2one;
2   
3   import java.io.Serializable;
4   import javax.persistence.*;
5   /**
6    * This class provides an example primary key class for a child entity that
7    * derives one of its primary key values from its parent entity in a many-to-one
8    * relationship.
9    */
10  @SuppressWarnings("serial")
11  @Embeddable
12  public class ItemPK implements Serializable {
13  	@Column(name="TYPE_ID_PK")
14  	private int typeId; //unique value from parent ItemType.id	
15  	@Column(name="NUMBER_PK")
16  	private int number; //unique value assigned to instances of Item
17  
18  	public int getTypeId() { return typeId; }
19  	public ItemPK setTypeId(int typeId) {
20  		this.typeId = typeId;
21  		return this;
22  	}	
23  	public int getNumber() { return number; }
24  	public ItemPK setNumber(int number) {
25  		this.number = number;
26  		return this;
27  	}
28  	
29  	@Override
30  	public int hashCode() {
31  		return typeId + number;
32  	}	
33  	@Override
34  	public boolean equals(Object obj) {
35  		try {
36  			if (this == obj) { return true; }
37  			ItemPK rhs = (ItemPK) obj;
38  			return typeId==rhs.typeId && number==rhs.number;
39  		} catch (Exception ex) { return false; }
40  	}
41  	@Override
42  	public String toString() {
43  		return "(typeId=" + typeId + ",number=" + number + ")";
44  	}
45  }