View Javadoc
1   package myorg.relex.one2manybi;
2   
3   import javax.persistence.*;
4   
5   /**
6    * This class provides an example of the many/owning side of a many-to-one, bi-directional 
7    * relationship mapped using a foreign key and that foreign key is used to derive the 
8    * primary key of this class.
9    */
10  @Entity
11  @Table(name="RELATIONEX_TIRE")
12  @IdClass(TirePK.class)
13  public class Tire {
14  	@Id
15  	@ManyToOne
16  	@JoinColumn(name="CAR_ID", nullable=false)
17  	private Car car;
18  	
19  	@Id @Enumerated(EnumType.STRING)
20  	@Column(length=16)
21  	private TirePosition position;
22  	
23  	private int miles;
24  	
25  	protected Tire() {}
26  	public Tire(Car car, TirePosition position) {
27  		this.car = car;
28  		this.position = position;
29  	}
30  
31  	public TirePosition getPosition() { return position; }
32  	public Car getCar() { return car; }
33  	
34  	public int getMiles() { return miles; }
35  	public void setMiles(int miles) {
36  		this.miles = miles;
37  	}
38  	
39  	@Override
40  	public int hashCode() {
41  		return position.hashCode();
42  	}
43  	@Override
44  	public boolean equals(Object obj) {
45  		try {
46  			if (this==obj) { return true; }
47  			Tire rhs = (Tire)obj;
48  			return car.equals(rhs.car) && position==rhs.position; 
49  		} catch (Exception ex) { return false; }
50  	}
51  }