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

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

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

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

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *