Hibernate實體映射技巧總結

來源:互聯網
上載者:User

標籤:hibernate   實體映射 技巧總結   

初學者有沒有感覺在寫Hibernate實習映射的時候,被各種的many-to-one set one-to-many搞亂了頭腦呢?

下面只需要三部,輕輕鬆鬆搞定。

1,寫注釋

格式為:(1?)屬性,是本類與(2?)的(3?)關係。

<span style="font-size:14px;">例如:<!-- users屬性,本類與User的一對多 --><!-- parent屬性,本類與Department的多對一 --><!-- roles屬性,本類與Role的多對多 --></span>

2,拷模板
<span style="font-size:14px;">一對多模板<set name=""><key column=""></key><one-to-many class=""/></set>多對一模板<many-to-one name="" class="" column=""></many-to-one>多對多模板<set name="" table=""><key column=""></key><many-to-many class="" column=""></many-to-many></set></span>


3,填空
<span style="font-size:14px;">name屬性:將(1?)填入class屬性:將(2?)填入特殊的column:當是:一對多 和 多對一 兩對對應關係時                多的一方維護關聯關係,也就是說多的一方有一的一方的外鍵。將"多對一"的一方的name+Id填入到column中,在將column中的值填入到"一對多"的column中例如: <!-- users屬性,本類與User的一對多 -->                <set name="users">            <key column="departmentId"></key>            <one-to-many class="User"/>                </set><!-- department屬性,本類與Department的多對一 --><many-to-one name="department" class="Department" column="departmentId"></many-to-one>         當是:多對多 和 多對多 的對應關係時有一個中間表維護雙方關係,也就是中間表中存放著雙方表的外鍵。所以table屬性的值,雙方要一致。        key中的column屬性代表的是本方主鍵對應到中間表的外鍵名。many-to-many的column屬性代表對方主鍵對應到中間表的外鍵名。例如:<!-- roles屬性,本類與Role的多對多 --><set name="roles" table="user_role"><key column="userId"></key><many-to-many class="Role" column="roleId"></many-to-many></set><!-- users屬性,本類與User的多對多 --><set name="users" table="user_role"><key column="roleId"></key><many-to-many class="User" column="userId"></many-to-many></set></span>




聯繫我們

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