隨筆-126  評論-247  文章-5  trackbacks-0

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

           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(truetrue);
          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     }



            
          posted on 2012-10-21 09:45 fancydeepin 閱讀(2341) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 手游| 潜江市| 海南省| 西吉县| 疏勒县| 鄄城县| 贺兰县| 嘉祥县| 井冈山市| 波密县| 政和县| 荣昌县| 札达县| 革吉县| 清远市| 陆丰市| 山阳县| 双牌县| 济南市| 保亭| 宜兴市| 西吉县| 荆州市| 阿鲁科尔沁旗| 开阳县| 怀宁县| 蓬安县| 朝阳县| 锦屏县| 虎林市| 南木林县| 尚志市| 英德市| 洛川县| 孟村| 武宁县| 铜鼓县| 汶上县| 上虞市| 蕲春县| 蚌埠市|