隨筆 - 0, 文章 - 75, 評論 - 0, 引用 - 0
          數據加載中……

          Hibernate關聯映射2


          單向一對多關聯:


          由“一”
          的一端加載“多”
          的一端,關系由“一”的一端來維護
          在JavaBean中是在“一”的一端中持有“多”的一端的集合
          Hibernate把這種關系反映到數據庫的策略是在“多”的一端的表上加一個外鍵指向“一”的一端的表
          在“一”的一端維護關系是不提倡的
          將“多”的一端的外鍵添加非空約束,導致數據不能插入
          插入數據效率降低


          反過來看,一個區可以有多個街道


          public
          class Street implements
          java.io.Serializable
          {
          //街道


          private
          Long id;


          private
          Long
          district_id;


          private
          String name;



          /**
          默認的構造方法
          *
          /


          public
          Street(){



          }



          //
          省略部分setter/getter方法


          public
          Long
          getDistrict_id(){



          return
          district_id;


          }


          public void
          setDistrict_id(Long
          district_id)
          {



          this.district_id
          =
          district_id;


          }


          }




          public
          class District implements
          java.io.Serializable
          {
          //區


          private
          Long id;


          private
          String name;


          private
          Set streets = new
          HashSet();



          /**
          默認的構造方法
          *
          /


          public
          District(){



          }



          //
          省略部分setter/getter方法


          public
          Set
          getStreets(){



          return
          this.streets;



          }


          public void setStreets(Set
          streets)
          {



          this.streets
          =
          streets
          ;



          }


          }




          <hibernate-mapping>


          <class name="cn.jbit.houserent.bean.District"
          table="
          district">



          <id name="id" type="
          java.lang.Long">



          <column name="id" />



          <generator class="native" />



          </id>



          <property name="name" type="
          java.lang.String">



          <column name="name" length="50" not-null="true" />



          </property>



          <set
          name="streets" table="street“>



          <key>



          <column name="
          district_id"/><!-- 設置關系的字段 -->



          </key>



          <one-to-many class="
          cn.jbit.houserent.bean.Street"
          /><!-- 設置關系的對象 -->



          </set>



          </class>


          </hibernate-mapping>




          set元素的常用屬性:



































          屬性


          含義和作用


          必須


          默認值


          name


          映射類屬性的名稱


          Y



          table


          關聯類的目標數據庫表


          N



          lazy


          指定關聯對象是否使用延遲加載以及延遲加載的策略


          N


          proxy


          fetch


          設置抓取數據的策略


          N


          select




          同時配置兩者就成了雙向一對多關聯

          posted on 2012-04-22 15:49 hantai 閱讀(54) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 雷山县| 灵山县| 清远市| 宽城| 稻城县| 乐陵市| 清水县| 喀喇沁旗| 随州市| 同仁县| 博野县| 铅山县| 宁晋县| 静海县| 武义县| 丹东市| 舟山市| 昌黎县| 宁强县| 平凉市| 清远市| 修武县| 常州市| 介休市| 德昌县| 蒙阴县| 襄垣县| 澄江县| 祁门县| 吴川市| 绥芬河市| 聂拉木县| 靖宇县| 陇川县| 同仁县| 庆安县| 明水县| 孟津县| 昆山市| 大连市| 信丰县|