JPA中,一对多关系使用@OneToMany标示
关系维护端:
1 package com.yl.demo1.bean.oneTomany;
2
3 import javax.persistence.CascadeType;
4 import javax.persistence.Column;
5 import javax.persistence.Entity;
6 import javax.persistence.GeneratedValue;
7 import javax.persistence.Id;
8 import javax.persistence.JoinColumn;
9 import javax.persistence.ManyToOne;
10
11
12 @Entity
13 public class OrderItem {
14
15 private Integer id;
16 private String productName;
17 private Float sellPrice;
18 private Order order;
19
20 @Id @GeneratedValue
21 public Integer getId() {
22 return id;
23 }
24 public void setId(Integer id) {
25 this.id = id;
26 }
27 @Column(length=40, nullable=false)
28 public String getProductName() {
29 return productName;
30 }
31 public void setProductName(String productName) {
32 this.productName = productName;
33 }
34 @Column(nullable=false)
35 public Float getSellPrice() {
36 return sellPrice;
37 }
38 public void setSellPrice(Float sellPrice) {
39 this.sellPrice = sellPrice;
40 }
41 /**
42 * optional代表属性是否可选,
43 * true--代表字段可为空
44 * false--代表字段不允许为null
45 *
46 * @JoinColumn---指定外键的名称
47 */
48 @ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH}, optional=true)
49 @JoinColumn(name="order_id")
50 public Order getOrder() {
51 return order;
52 }
53 public void setOrder(Order order) {
54 this.order = order;
55 }
56
57
58 }
关系被维护端:
1 package com.yl.demo1.bean.oneTomany;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import javax.persistence.CascadeType;
7 import javax.persistence.Column;
8 import javax.persistence.Entity;
9 import javax.persistence.FetchType;
10 import javax.persistence.Id;
11 import javax.persistence.OneToMany;
12 import javax.persistence.Table;
13 /**
14 * JPA中,1-m的双向关系中,多的一方为关系维护端,关系维护端服务外键记录的更新,关系被维护端是没有权利更新外键记录的
15 *
16 *
17 */
18 @Entity
19 @Table(name="orders")
20 public class Order {
21 private String orderId;
22 private Float amount = 0f;
23 private Set<OrderItem> items = new HashSet<OrderItem>();
24
25 @Id @Column(length=12)
26 public String getOrderId() {
27 return orderId;
28 }
29 public void setOrderId(String orderId) {
30 this.orderId = orderId;
31 }
32 @Column(nullable=false)
33 public Float getAmount() {
34 return amount;
35 }
36 public void setAmount(Float amount) {
37 this.amount = amount;
38 }
39 /**级联操作
40 * CascadeType.REFRESH--select order, select OrderItem
41 * CascadeType.PERSIST--insert into Order, insert into OrderItem
42 * CascadeType.MERGE----当Order处于游离状态时,对Order更新,也会更新OrderItem--update Order, for(update OrderItem)
43 * CascadeType.REMOVE---delete Order, delete OrderItem
44 * 可以采用CascadeType.ALL包括上述四种.
45 * 上述四种方法只有调用相应的方法时才会触发。即EntityManager.refresh(),EntityManager.persist(),EntityManager.merge(), EntityManager.remove()
46 *
47 * *ToMany的fetch默认值为FetchType.LAZY
48 * *ToOne的fetch默认值为FetchType.EAGER
49 *
50 *
51 * mappedBy出现在关系的被维护端,它指定在关系维护端是由哪个属性进行维护关系
52 */
53 @OneToMany(cascade={CascadeType.REFRESH, CascadeType.PERSIST}
54 , fetch=FetchType.LAZY
55 , mappedBy="order")
56 public Set<OrderItem> getItems() {
57 return items;
58 }
59 public void setItems(Set<OrderItem> items) {
60 this.items = items;
61 }
62
63
64 public void addOrderItem(OrderItem orderItem) {
65 orderItem.setOrder(this);
66 this.items.add(orderItem);
67 }
68
69 }
常见操作:
1 @Test
2 public void save() {
3 EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");
4 EntityManager em = factory.createEntityManager();
5 em.getTransaction().begin();//事务开始
6
7 Order order = new Order();
8 order.setAmount(34f);
9 order.setOrderId("999");
10
11 OrderItem orderItem1 = new OrderItem();
12 orderItem1.setProductName("football");
13 orderItem1.setSellPrice(90f);
14
15 OrderItem orderItem2 = new OrderItem();
16 orderItem2.setProductName("basketball");
17 orderItem2.setSellPrice(100f);
18
19 order.addOrderItem(orderItem1);
20 order.addOrderItem(orderItem2);
21
22 em.persist(order);
23
24 em.getTransaction().commit();
25 em.close();
26 factory.close();
27 }