码迷,mamicode.com
首页 > Web开发 > 详细

转@OneToMany或@ManyToOne的用法-annotation关系映射篇(上)

时间:2016-11-28 15:20:02      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:items   float   man   tar   div   bean   加载   obd   except   

原文:http://blog.sina.com.cn/s/blog_6fef491d0100obdm.html

例如我们用一个例子来开启JPA的一对多和多对一的学习。

 

比如你去当当网上买书籍,当当网就给你一个订单。
  通过上面的业务活动描述你可以分析得出:一个订单可以包含一个或多个订单项.那么我们将将订单和订单项设计关系为(1:N)一对多的关系(排除0)。得出关系我们就使用JPA来实现这个关系(关于建立JPA和基本的配置我就不再说了,如果不明白请看JPA入门篇)。

首先建立订单实体类
 1 import java.util.HashSet;   
 2 import java.util.Set;   
 3   
 4 import javax.persistence.CascadeType;   
 5 import javax.persistence.Column;   
 6 import javax.persistence.Entity;   
 7 import javax.persistence.Id;   
 8 import javax.persistence.OneToMany;   
 9 import javax.persistence.Table;   
10   
11 @Entity  
12 @Table(name="orders")   
13 public class Order {   
14        
15     private String orderid;   
16     private Float amount = 0f;   
17     private Set<OrderItem> items = new HashSet<OrderItem>();   
18   
19     @Id  
20     @Column(length = 12)   
21     public String getOrderid() {   
22         return orderid;   
23     }   
24   
25     public void setOrderid(String orderid) {   
26         this.orderid = orderid;   
27     }   
28   
29     @Column(nullable = false)   
30     public Float getAmount() {   
31         return amount;   
32     }   
33   
34     public void setAmount(Float amount) {   
35         this.amount = amount;   
36     }   
37   
38     @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE },mappedBy ="order") //这里配置关系,并且确定关系维护端和被维护端。mappBy表示关系被维护端,只有关系端有权去更新外键。这里还有注意OneToMany默认的加载方式是赖加载。当看到设置关系中最后一个单词是Many,那么该加载默认为懒加载   
39     public Set<OrderItem> getItems() {   
40         return items;   
41     }   
42   
43     public void setItems(Set<OrderItem> items) {   
44         this.items = items;   
45     }   
46        
47          /**   
48            *该方法用于向order中加order项   
49           /*   
50     public void addOrderItem(OrderItem orderItem){   
51         orderItem.setOrder(this);//用关系维护端来维护关系   
52         this.items.add(orderItem);   
53     }   
54   
55 }  

订单项的实体类

 1 import javax.persistence.CascadeType;   
 2 import javax.persistence.Column;   
 3 import javax.persistence.Entity;   
 4 import javax.persistence.GeneratedValue;   
 5 import javax.persistence.Id;   
 6 import javax.persistence.JoinColumn;   
 7 import javax.persistence.ManyToOne;   
 8   
 9 @Entity  
10 public class OrderItem {   
11     private Integer id;   
12     private String productName;   
13     private Float sellPrice = 0f;   
14     private Order order;   
15   
16     @Id  
17     @GeneratedValue  
18     public Integer getId() {   
19         return id;   
20     }   
21   
22     public void setId(Integer id) {   
23         this.id = id;   
24     }   
25   
26     @Column(length = 40, nullable = false)   
27     public String getProductName() {   
28         return productName;   
29     }   
30   
31     public void setProductName(String productName) {   
32         this.productName = productName;   
33     }   
34   
35     @Column(nullable = false)   
36     public Float getSellPrice() {   
37         return sellPrice;   
38     }   
39   
40     public void setSellPrice(Float sellPrice) {   
41         this.sellPrice = sellPrice;   
42     }   
43   
44     @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH }, optional = true)   
45     @JoinColumn(name="order_id")//这里设置JoinColum设置了外键的名字,并且orderItem是关系维护端   
46     public Order getOrder() {   
47         return order;   
48     }   
49   
50     public void setOrder(Order order) {   
51         this.order = order;   
52     }   
53   
54 }  

下面看看测试类

 1 import javax.persistence.EntityManager;   
 2 import javax.persistence.EntityManagerFactory;   
 3 import javax.persistence.Persistence;   
 4   
 5 import org.junit.BeforeClass;   
 6 import org.junit.Test;   
 7   
 8 import cn.itcast.bean.Order;   
 9 import cn.itcast.bean.OrderItem;   
10   
11 public class OneToManyTest {   
12   
13     @BeforeClass  
14     public static void setUpBeforeClass() throws Exception {   
15     }   
16     @Test public void addOrder(){   
17          EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");   
18          EntityManager em = factory.createEntityManager();   
19          em.getTransaction().begin(); // start transaction   
20             
21                    Order order = new Order();    
22          order.setAmount(34f);    
23          order.setOrderid("00001");   
24                       
25                    //order中包含的OrderItem项OrderItem1,OrderItem2   
26          OrderItem orderItem1 = new OrderItem();   
27          orderItem1.setProductName("书");   
28          orderItem1.setSellPrice(22f);   
29          order.addOrderItem(orderItem1); //add orderitem in order   
30             
31          OrderItem orderItem2 = new OrderItem();   
32          orderItem2.setProductName("篮球");   
33          orderItem2.setSellPrice(100f);   
34          order.addOrderItem(orderItem2);   
35             
36          em.persist(order); //persist order object   
37          em.getTransaction().commit(); //commit transaction   
38          em.close();   
39          factory.close();   
40     }   
41 }  

 

 

转@OneToMany或@ManyToOne的用法-annotation关系映射篇(上)

标签:items   float   man   tar   div   bean   加载   obd   except   

原文地址:http://www.cnblogs.com/linvan/p/6109408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!