关于JPA一对多关联这里使用Order与OrderItem来模拟。一个Order可以关联多个OrderItem,而一个OrderItem只能关联一个Order。Order与OrderItem是一对多的关联关系。Order类如下:

 package com.yichun.bean;

 import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table; @Entity
@Table(name = "orders")
public class Order { private String orderId;
private Float amoutn = 0f;
private Set<OrderItem> items = new HashSet<OrderItem>(); @Id
@Column(length = 12, nullable = false)
public String getOrderId() {
return orderId;
} public void setOrderId(String orderId) {
this.orderId = orderId;
} @Column(nullable = false)
public Float getAmoutn() {
return amoutn;
} public void setAmoutn(Float amoutn) {
this.amoutn = amoutn;
} // CascadeType.REFRESH,级联刷新。orderItems更改的时候,就重新加载orderItems // CascadeType.PERSIST, 级联保存。insert order insert into orderitem 。调用manager.persist(new Order())的时候才会起作用 // CascadeType.MERGE,级联更新 。order在游离状态的时候被修改了,order中的orderItem也被修改了,update order for (update orderItem)。 调用manager.merge(order) 的时候才会起作用 // CascadeType.REMOVE级联删除。 删除order的时候,orderItems也会被删除,没有住外键约束的时候。调用manager.remove(order)的时候才会起作用。 // 针对实体管理器的四种方法
// fetch =FetchType.LAZY默认是延迟加载。××tomany由一方得到多的一方的时候,默认就是延迟加载,××toone的时候,默认就是立即加载
// 出现mappedBy的类就是关系被维护端。关系由对方里面的order属性维护
@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,
CascadeType.MERGE, CascadeType.REMOVE }, mappedBy = "order")
public Set<OrderItem> getItems() {
return items;
} public void setItems(Set<OrderItem> items) {
this.items = items;
} // 建立两者之间的关系
public void addOrderItem(OrderItem orderItem) {
orderItem.setOrder(this);
this.items.add(orderItem);
}
}

OrderItem类如下:

 package com.yichun.bean;

 import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; @Entity
public class OrderItem {
private Integer id;
private String productName;
private Float sellPrice = 0f;
private Order order; @Id
@GeneratedValue
public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} @Column(length = 40, nullable = false)
public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
} @Column(nullable = false)
public Float getSellPrice() {
return sellPrice;
} public void setSellPrice(Float sellPrice) {
this.sellPrice = sellPrice;
} // optional=true,在数据库中允许为null
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = true)// 级联更新,级联刷新
@JoinColumn(name = "order_id")
public Order getOrder() {
return order;
} public void setOrder(Order order) {
this.order = order;
}
}

保存数据:

     @Test
public void save() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("testjpa");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin(); Order order = new Order();
order.setAmoutn(120f);
order.setOrderId("999");// UUID.randomUUID().toString() OrderItem item = new OrderItem();
item.setProductName("足球");
item.setSellPrice(90f); OrderItem item2 = new OrderItem();
item2.setProductName("排球");
item2.setSellPrice(30f); // 设置两者之间的关联
order.addOrderItem(item);
order.addOrderItem(item2); // 保存Order,此时也会保持OrderItem
manager.persist(order); manager.getTransaction().commit();
manager.clear();
factory.close();
}
05-11 00:10