JPA一对多关联
关于JPA一对多关联这里使用Order与OrderItem来模拟。一个Order可以关联多个OrderItem,而一个OrderItem只能关联一个Order。Order与OrderItem是一对多的关联关系。Order类如下:
1 package com.yichun.bean; 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.Id; 10 import javax.persistence.OneToMany; 11 import javax.persistence.Table; 12 13 @Entity 14 @Table(name = "orders") 15 public class Order { 16 17 private String orderId; 18 private Float amoutn = 0f; 19 private Set<OrderItem> items = new HashSet<OrderItem>(); 20 21 @Id 22 @Column(length = 12, nullable = false) 23 public String getOrderId() { 24 return orderId; 25 } 26 27 public void setOrderId(String orderId) { 28 this.orderId = orderId; 29 } 30 31 @Column(nullable = false) 32 public Float getAmoutn() { 33 return amoutn; 34 } 35 36 public void setAmoutn(Float amoutn) { 37 this.amoutn = amoutn; 38 } 39 40 // CascadeType.REFRESH,级联刷新。orderItems更改的时候,就重新加载orderItems 41 42 // CascadeType.PERSIST, 级联保存。insert order insert into orderitem 。调用manager.persist(new Order())的时候才会起作用 43 44 // CascadeType.MERGE,级联更新 。order在游离状态的时候被修改了,order中的orderItem也被修改了,update order for (update orderItem)。 调用manager.merge(order) 的时候才会起作用 45 46 // CascadeType.REMOVE级联删除。 删除order的时候,orderItems也会被删除,没有住外键约束的时候。调用manager.remove(order)的时候才会起作用。 47 48 // 针对实体管理器的四种方法 49 // fetch =FetchType.LAZY默认是延迟加载。××tomany由一方得到多的一方的时候,默认就是延迟加载,××toone的时候,默认就是立即加载 50 // 出现mappedBy的类就是关系被维护端。关系由对方里面的order属性维护 51 @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST, 52 CascadeType.MERGE, CascadeType.REMOVE }, mappedBy = "order") 53 public Set<OrderItem> getItems() { 54 return items; 55 } 56 57 public void setItems(Set<OrderItem> items) { 58 this.items = items; 59 } 60 61 // 建立两者之间的关系 62 public void addOrderItem(OrderItem orderItem) { 63 orderItem.setOrder(this); 64 this.items.add(orderItem); 65 } 66 }
OrderItem类如下:
1 package com.yichun.bean; 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 @Entity 12 public class OrderItem { 13 private Integer id; 14 private String productName; 15 private Float sellPrice = 0f; 16 private Order order; 17 18 @Id 19 @GeneratedValue 20 public Integer getId() { 21 return id; 22 } 23 24 public void setId(Integer id) { 25 this.id = id; 26 } 27 28 @Column(length = 40, nullable = false) 29 public String getProductName() { 30 return productName; 31 } 32 33 public void setProductName(String productName) { 34 this.productName = productName; 35 } 36 37 @Column(nullable = false) 38 public Float getSellPrice() { 39 return sellPrice; 40 } 41 42 public void setSellPrice(Float sellPrice) { 43 this.sellPrice = sellPrice; 44 } 45 46 // optional=true,在数据库中允许为null 47 @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = true)// 级联更新,级联刷新 48 @JoinColumn(name = "order_id") 49 public Order getOrder() { 50 return order; 51 } 52 53 public void setOrder(Order order) { 54 this.order = order; 55 } 56 }
保存数据:
1 @Test 2 public void save() { 3 EntityManagerFactory factory = Persistence 4 .createEntityManagerFactory("testjpa"); 5 EntityManager manager = factory.createEntityManager(); 6 manager.getTransaction().begin(); 7 8 Order order = new Order(); 9 order.setAmoutn(120f); 10 order.setOrderId("999");// UUID.randomUUID().toString() 11 12 OrderItem item = new OrderItem(); 13 item.setProductName("足球"); 14 item.setSellPrice(90f); 15 16 OrderItem item2 = new OrderItem(); 17 item2.setProductName("排球"); 18 item2.setSellPrice(30f); 19 20 // 设置两者之间的关联 21 order.addOrderItem(item); 22 order.addOrderItem(item2); 23 24 // 保存Order,此时也会保持OrderItem 25 manager.persist(order); 26 27 manager.getTransaction().commit(); 28 manager.clear(); 29 factory.close(); 30 }