hibernate annotation 之 多對一單向外鍵關聯 – JAVA編程語言程序開發技術文章

假設,一個農場產出多種植物,具體的某一植物產於某一農場。

 1 package net.yeah.fancydeepin.po;
 2
 3 import java.io.Serializable;
 4 import javax.persistence.Column;
 5 import javax.persistence.Entity;
 6 import javax.persistence.GeneratedValue;
 7 import javax.persistence.Id;
 8 /**
 9  * —————————————–
10  * @描述  農場
11  * @作者  fancy
12  * @郵箱  fancydeepin@yeah.net
13  * @日期  2012-10-21 <p>
14  * —————————————–
15  */
16 @Entity
17 public class Farm implements Serializable{
18
19     private static final long serialVersionUID = 1L;
20    
21     private Integer id;
22     private String  name;
23    
24     @Id
25     @GeneratedValue
26     public Integer getId() {
27         return id;
28     }
29     @Column(length = 18, nullable = false)
30     public String getName() {
31         return name;
32     }
33     public void setId(Integer id) {
34         this.id = id;
35     }
36     public void setName(String name) {
37         this.name = name;
38     }
39 }

 1 package net.yeah.fancydeepin.po;
 2
 3 import java.io.Serializable;
 4 import javax.persistence.CascadeType;
 5 import javax.persistence.Column;
 6 import javax.persistence.Entity;
 7 import javax.persistence.FetchType;
 8 import javax.persistence.GeneratedValue;
 9 import javax.persistence.Id;
10 import javax.persistence.ManyToOne;
11 /**
12  * —————————————–
13  * @描述  植物
14  * @作者  fancy
15  * @郵箱  fancydeepin@yeah.net
16  * @日期  2012-10-21 <p>
17  * —————————————–
18  */
19 @Entity
20 public class Plant implements Serializable{
21
22     private static final long serialVersionUID = 1L;
23    
24     private Integer id;
25     private String    name;
26     private Farm     farm;
27    
28     @Id
29     @GeneratedValue
30     public Integer getId() {
31         return id;
32     }
33     @Column(length = 18, nullable = false)
34     public String getName() {
35         return name;
36     }
37     @ManyToOne(cascade = CascadeType.PERSIST, optional = false, fetch = FetchType.LAZY)
38     public Farm getFarm() {
39         return farm;
40     }
41     public void setId(Integer id) {
42         this.id = id;
43     }
44     public void setName(String name) {
45         this.name = name;
46     }
47     public void setFarm(Farm farm) {
48         this.farm = farm;
49     }
50
51 }

Junit 測試 :

1 @Test
2     public void createTable(){
3        
4         new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
5     }

執行上面的單元測試,數據庫中生成的表的結構圖 :

 

級聯保存 :

 1     private static Session session;
 2    
 3     @BeforeClass
 4     public static void beforeClass(){
 5        
 6         session = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession();
 7     }
 8    
 9     @Test
10     public void insert(){
11         try {
12             Farm farm = new Farm();
13             farm.setName("fancy-farm");
14             Plant tomato  = new Plant();
15             tomato.setName("番茄");
16             tomato.setFarm(farm);
17             Plant cabbage = new Plant();
18             cabbage.setName("卷心菜");
19             cabbage.setFarm(farm);
20             session.beginTransaction();
21             session.persist(tomato);
22             session.persist(cabbage);
23             session.getTransaction().commit();
24         } catch (Exception e) {
25             e.printStackTrace();
26         }
27     }

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。