隨筆-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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 青海省| 永清县| 无棣县| 革吉县| 个旧市| 永胜县| 兰坪| 广东省| 喀什市| 漠河县| 合山市| 灵川县| 察雅县| 澄城县| 呼图壁县| 惠水县| 咸丰县| 阆中市| 湘阴县| 莱芜市| 九江市| 北宁市| 吐鲁番市| 渭源县| 武穴市| 淮北市| 巴楚县| 永定县| 泸水县| 云霄县| 姚安县| 兴化市| 县级市| 且末县| 肇东市| 大连市| 光山县| 大足县| 东莞市| 德昌县| 会泽县|