View Javadoc
1   package myorg.relex.one2manybi;
2   
3   import java.util.ArrayList;
4   import java.util.Date;
5   import java.util.List;
6   import javax.persistence.*;
7   /**
8    * This class provides an example of the one/inverse side of a one-to-many, bi-directional 
9    * relationship realized through a join-table mapped from the owning/many side.
10   */
11  @Entity
12  @Table(name="RELATIONEX_PURCHASE")
13  public class Purchase {
14  	@Id @GeneratedValue
15  	private int id;
16  	
17  	@OneToMany(
18  			mappedBy="purchase", 
19  			cascade={CascadeType.PERSIST, CascadeType.DETACH},
20  			orphanRemoval=true
21  		)
22  	private List<SaleItem> items;
23  	
24  	@Temporal(TemporalType.TIMESTAMP)
25  	@Column(nullable=false, updatable=false)
26  	private Date date;
27  	
28  	protected Purchase() {}
29  	public Purchase(Date date) {
30  		this.date = date;
31  	}
32  
33  	public int getId() { return id; }
34  	public Date getDate() { return date; }
35  	public List<SaleItem> getItems() {
36  		if (items == null) {
37  			items = new ArrayList<SaleItem>();
38  		}
39  		return items;
40  	}
41  	public Purchase addItem(SaleItem item) {
42  		getItems().add(item);
43  		return this;
44  	}
45  }