hibernate通過xml設定檔實現表與實體的映射

來源:互聯網
上載者:User

標籤:

這裡討論的是一對多的關係

在做公交卡系統,會涉及到兩張表,忽略兩種表的作用,只關心他們之間的關係 : 卡規格表和卡類表,一種卡規格會對應多個卡類

實體類:

/*** 卡類型表的實體*/public class Acardtype  implements java.io.Serializable {                   private Long id;        private Cardstandard cardstandard;   //卡規格表外鍵     .........
/***卡規格表實體*/public class Cardstandard  implements java.io.Serializable {     private Long id;     private Set acardtypes = new HashSet( );    //此集合用來存卡類型
....

xml映射:

 <-- 卡類型的xml映射 --><hibernate-mapping>    <class name="com.foorich.entity.oracle.Acardtype" table="ACARDTYPE" schema="FOORICH_CARD">        <id name="id" type="java.lang.Long">            <column name="ID" />            <generator class="increment"></generator>   <-- 主鍵 -->        </id>            <-- 映射關係屬性,          many-to-one:卡類型是多的一方,卡規格是一的一方           fetch:查詢時忽略懶載入          column:指名卡類型表的外鍵名稱    -->         <many-to-one name="cardstandard" class="com.foorich.entity.oracle.Cardstandard" fetch="select">            <column name="CARDSTANDARDID" />        </many-to-one>
  </class>
<hibernate-mapping>
<-- 卡規格表的對應檔 --><hibernate-mapping>    <class name="com.foorich.entity.oracle.Cardstandard" table="CARDSTANDARD" schema="FOORICH_CARD">        <id name="id" type="java.lang.Long">            <column name="ID" />            <generator class="increment" />        </id>          <-- 集合映射      inverse:可以通過卡規格表找到卡類型表      key:告知,卡類型表中的外鍵      one-to-many: 告知它的關係表是卡類型表   -->        <set name="acardtypes" inverse="true">            <key>                <column name="CARDSTANDARDID" />            </key>            <one-to-many class="com.foorich.entity.oracle.Acardtype" />        </set>    </class></hibernate-mapping>

 

hibernate通過xml設定檔實現表與實體的映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.