例如我们用一个例子来开启JPA的一对多和多对一的学习。
比如你去当当网上买书籍,当当网就给你一个订单。
通过上面的业务活动描述你可以分析得出:一个订单可以包含一个或多个订单项.那么我们将将订单和订单项设计关系为(1:N)一对多的关系(排除0)。
得出关系我们就使用JPA来实现这个关系(关于建立JPA和基本的配置我就不再说了,如果不明白请看
JPA入门篇
)。
首先建立订单实体类
订单项的实体类
下面看看测试类
首先建立订单实体类
- 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 amount = 0f; -
private Set<OrderItem> items = new HashSet<OrderItem>(); -
-
@Id -
@Column(length = 12) -
public String getOrderid() { -
return orderid; -
} -
-
public void setOrderid(String orderid) { -
this.orderid = orderid; -
} -
-
@Column(nullable = false) -
public Float getAmount() { -
return amount; -
} -
-
public void setAmount(Float amount) { -
this.amount = amount; -
} -
-
@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE },mappedBy ="order") //这里配置关系,并且确定关系维护端和被维护端。mappBy表示关系被维护端,只有关系端有权去更新外键。这里还有注意OneToMany默认的加载方式是赖加载。当看到设置关系中最后一个单词是Many,那么该加载默认为懒加载 -
public Set<OrderItem> getItems() { -
return items; -
} -
-
public void setItems(Set<OrderItem> items) { -
this.items = items; -
} -
-
/** -
*该方法用于向order中加order项 -
/* -
public void addOrderItem(OrderItem orderItem){ -
orderItem.setOrder(this);//用关系维护端来维护关系 -
this.items.add(orderItem); -
} -
- }
订单项的实体类
- 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; -
} -
-
@ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH }, optional = true) -
@JoinColumn(name="order_id")//这里设置JoinColum设置了外键的名字,并且orderItem是关系维护端 -
public Order getOrder() { -
return order; -
} -
-
public void setOrder(Order order) { -
this.order = order; -
} -
- }
下面看看测试类
- import
javax.persistence.EntityManager; - import
javax.persistence.EntityManagerFactory; - import
javax.persistence.Persistence; -
- import
org.junit.BeforeClass; - import
org.junit.Test; -
- import
cn.itcast.bean.Order; - import
cn.itcast.bean.OrderItem; -
- public
class OneToManyTest { -
-
@BeforeClass -
public static void setUpBeforeClass() throws Exception { -
} -
@Test public void addOrder(){ -
EntityManagerFactory factory = Persistence.createEntityManagerFacto ry("test"); -
EntityManager em = factory.createEntityManager(); -
em.getTransaction().begin(); // start transaction -
-
Order order = new Order(); -
order.setAmount(34f); -
order.setOrderid("00001"); -
-
//order中包含的OrderItem项OrderItem1,OrderItem2 -
OrderItem orderItem1 = new OrderItem(); -
orderItem1.setProductName("书"); -
orderItem1.setSellPrice(22f); -
order.addOrderItem(orderItem1); //add orderitem in order -
-
OrderItem orderItem2 = new OrderItem(); -
orderItem2.setProductName("篮球"); -
orderItem2.setSellPrice(100f); -
order.addOrderItem(orderItem2); -
-
em.persist(order); //persist order object -
em.getTransaction().commit(); //commit transaction -
em.close(); -
factory.close(); -
} - }