6.1.1 有串連表的單向N—>1關聯

來源:互聯網
上載者:User

對於絕大部分單向N--->1關聯而言,使用基於外鍵的關聯映射已經足夠了。但由於底層資料庫建模時也可以使用串連表來建立這種關聯關係,因此Hibernate也為這種關聯關係提供了支援。

如果需要使用串連表來映射單向N--->1關聯,則需要顯式使用<join.../>元素,該元素用於強制將一個類的屬性對應到多張表中,通常也用於強制使用串連表。使用<join.../>元素時需要指定一個table屬性,用於指定串連表的表名。

本篇文章承接上一篇文章。

我們先將test庫裡的表刪除:

除了Person.hbm.xml檔案需要修改,其餘所有檔案不變。

Person.hbm.xml :

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    <hibernate-mapping package="db.domain">    <class name="Person" table="persons">            <id name="id" type="integer">            <generator class="identity"></generator>        </id>        <property name="name" type="string">            <column name="name"></column>        </property>        <property name="age" type="integer">            <column name="age"></column>        </property>        <join table="person_address">            <key column="person_id"/>            <many-to-one name="address" class="Address"                 column="address_id" cascade="all"/>        </join>                    </class></hibernate-mapping>

運行Test.java,查看資料庫:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.