Hibernate一對多(多對一)外鍵設定匯總

來源:互聯網
上載者:User

標籤:

 

我打算在角色表(role)中添加一個帳號表(account)的外鍵(accountId),步驟如下:

1、首先在角色表(role)中添加列。

         添加語句:alter table role add(accountid varchar2(50));

         添加語句時注意事項:單詞之間的空格必須為英文空格,不可為中文空格;accountId添加後不可
         設為主鍵!

2、將accountId這一列設為外鍵,連結角色表(role)和帳號表(account)。 
         添加語句:alter table role add constraint fk_role_account foreign key(accountId)

         references account(id) on delete cascade;

         或:

         alter table role add constraint fk_role_account foreign key(accountId) references

         account(id) on delete set null;

    解釋:constraint:約束,即外鍵約束。

          fk_role_account:外鍵名稱。

          foreign key(accountId):將accountId設為外鍵。

          references:參照。

          account(id):account,帳號表明;id,外鍵accountId的參照列。

          on delete cascade和on delete set null:兩個要害字,第一個的意思就是當帳號刪除的時候,
         所有角色表中accountId與刪除帳號id相同的角色資訊刪除;第二個的意思就是當帳號刪除的時候,所

         有角色表中accountId與刪除帳號id相同的角色accountId這一列清空。

3、在role.hbm.xml設定檔中添加多對一關聯性   
   代碼如下:

        <many-to-one name="account" class="com.yuanit.app.model.Account" update="false"
        insert="false" fetch="select">
                  <column name="ACCOUNTID" length="50" />
        </many-to-one>
   解釋:
        <many-to-one name="對應一對多中一的實體類的集合的屬性" class="一對多中一的實體類

        的類名" update="false" insert="false" fetch="select">
                  <column name="對應其外鍵的欄位" length="50" />
        </many-to-one>

4、在account.hbm.xml設定檔中添加一對多關聯性

   代碼如下:

        <set name="roles" inverse="true" lazy="false" >
             <key>
                 <column name="ACCOUNTID" length="50"/>
             </key>
             <one-to-many class="com.yuanit.app.model.Role"/>
        </set>
    解釋:
        <set name="對應一對多中多的實體類的集合的屬性" inverse="true" lazy="false" >
             <key>
                 <column name="對應其外鍵的欄位" length="50"/>
             </key>
             <one-to-many class="一對多中多的實體類的類名"/>
       </set>
5、在role的model中添加account集合及其set/get方法:

       private Account account;

       public void setAccount(Account account) {
           this.account = account;
       }

       public Account getAccount() {
           return account;
       }
6、在account的model中添加roles集合及其set/get方法:

       private Set<Role> roles = new HashSet<Role>(0);

       public void setRoles(Set<Role> roles) {
          this.roles = roles;
       }

       public Set<Role> getRoles() {
          return roles;
       }

Hibernate一對多(多對一)外鍵設定匯總

聯繫我們

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