View Javadoc
1   package myorg.relex.one2one;
2   
3   import javax.persistence.*;
4   
5   /**
6    * This class demonstrates a one-to-one, uni-directional relationship
7    * where the foreign key is used to define the primary key with the
8    * use of @MapsId
9    */
10  @Entity
11  @Table(name="RELATIONEX_COACH")
12  public class Coach {
13  	public enum Type {HEAD, ASSISTANT };
14  	@Id //provider sets to FK value with help from @MapsId 
15  	private int id;
16  
17  	@OneToOne(optional=false, fetch=FetchType.EAGER)
18  	@MapsId //informs provider the PK is derived from FK
19  	private Person person;
20  
21  	@Enumerated(EnumType.STRING) @Column(length=16)
22  	private Type type;
23  
24  	public Coach() {}	
25  	public Coach(Person person) {
26  		this.person = person;
27  	}
28  	
29  	public int getId() { return person==null ? 0 : person.getId(); }
30  	public Person getPerson() { return person; }
31  
32  	public Type getType() { return type; }
33  	public void setType(Type type) {
34  		this.type = type;
35  	}
36  }