首先建立订单实体类
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 items = new HashSet();
@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 getItems() {
return items;
}
public void setItems(Set 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.createEntityManagerFactory("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();
}
}
双向一对多关系,一是关系维护端(owner side),多是关系被维护端(inverse side)。在关系被维护端需要通过@JoinColumn建立外键列指向关系维护端的主键列。
http://blog.csdn.net/gebitan505/article/details/22619175
http://blog.csdn.net/lxl_family/article/details/26523757