View Javadoc
1   package myorg.relex.many2one;
2   
3   import java.util.Date;
4   
5   import javax.persistence.*;
6   
7   /**
8    * This class provides an example of a child entity that derives its primary key from 
9    * the parent/one side of a many-to-one relation.
10   */
11  @Entity
12  @Table(name="RELATIONEX_ITEM")
13  public class Item {
14  	@EmbeddedId
15  	private ItemPK id;
16  	
17  	@ManyToOne(optional=false)
18  	@MapsId("typeId") //refers to the ItemPK.typeId property
19  	@JoinColumn(name="TYPE_ID")
20  	private ItemType itemType;
21  	
22  	@Temporal(TemporalType.TIMESTAMP)
23  	private Date created;
24  	
25  	protected Item() {}
26  	public Item(ItemType itemType, int number) {		
27  		this.itemType = itemType;
28  			//typeId in PK auto-mapped to itemType FK
29  		this.id = new ItemPK().setNumber(number);
30  	}
31  
32  	public ItemPK getId() { return id; }
33  	public ItemType getItemType() { return itemType; }
34  
35  	public Date getCreated() { return created; }
36  	public void setCreated(Date created) {
37  		this.created = created;
38  	}
39  	
40  	@Override
41  	public String toString() {
42  		return (itemType==null?null:itemType) + "pk=" + id;
43  	}
44  }