View Javadoc
1   package myorg.relex.one2manybi;
2   
3   import java.util.Date;
4   import java.util.HashSet;
5   import java.util.Set;
6   
7   import javax.persistence.*;
8   
9   /**
10   * This class is an example of the one/inverse side of a one-to-many, bi-directional
11   * relationship mapped using a compound foreign key that is partially derived from the 
12   * parent primary key.
13   */
14  @Entity
15  @Table(name="RELATIONEX_CAR")
16  public class Car {
17  	@Id @GeneratedValue
18  	private int id;
19  	
20  	@OneToMany(
21  			mappedBy="car",
22  			cascade={CascadeType.PERSIST, CascadeType.DETACH}, 
23  			orphanRemoval=true,
24  			fetch=FetchType.LAZY)
25  	private Set<Tire> tires;
26  	
27  	@Column(length=16)
28  	private String model;
29  	@Temporal(TemporalType.DATE)
30  	private Date year;
31  
32  	public int getId() { return id; }
33  	public Set<Tire> getTires() {
34  		if (tires==null) {
35  			tires=new HashSet<Tire>();
36  		}
37  		return tires;
38  	}
39  
40  	public String getModel() { return model; }
41  	public void setModel(String model) {
42  		this.model = model;
43  	}
44  	
45  	public Date getYear() { return year; }
46  	public void setYear(Date year) {
47  		this.year = year;
48  	}
49  	
50  	@Override
51  	public int hashCode() {
52  		return (model==null?0:model.hashCode()) + (year==null?0:year.hashCode());
53  	}
54  	@Override
55  	public boolean equals(Object obj) {
56  		try {
57  			if (this==obj) { return true; }
58  			Car rhs = (Car)obj;
59  			return id==0 ? super.equals(obj) : id==rhs.id;
60  		} catch (Exception ex) { return true; }
61  	}
62  }