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

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

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



            
          posted on 2012-10-21 16:48 fancydeepin 閱讀(3350) 評論(1)  編輯  收藏

          評論:
          # re: hibernate annotation 之 一對多、多對一雙向外鍵關聯 2014-06-18 03:18 |
          相關的jar包呢?  回復  更多評論
            

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


          網站導航:
           
          主站蜘蛛池模板: 呼和浩特市| 万年县| 华坪县| 富蕴县| 隆尧县| 吉隆县| 三亚市| 思茅市| 巴彦县| 建始县| 双城市| 安丘市| 邢台县| 黄浦区| 灵石县| 三明市| 凉城县| 永和县| 延边| 绥江县| 开封市| 通化市| 绍兴县| 大洼县| 蓬安县| 高州市| 五大连池市| 泰来县| 石门县| 嵊州市| 贵定县| 长子县| 佛山市| 育儿| 蓬莱市| 神农架林区| 平凉市| 临江市| 木兰县| 鹿泉市| 壶关县|