Hibernate學習筆記_08_一對一單向、雙向主鍵關聯

來源:互聯網
上載者:User

主鍵關聯和外部索引鍵關聯類似,只需要將對應的註解稍作修改即可,主鍵關聯用的很少。


1、寫Husband.java類,並在其中添加對Wife的引用,同時註解。

package com.hibernate._0800_one2one_uni_pk;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToOne;import javax.persistence.PrimaryKeyJoinColumn;@Entitypublic class Husband {private Integer id;private String name;private Wife wife;  //去參考wife的id,此時wife必須先id@Id@GeneratedValuepublic Integer getId() {return id;}public String getName() {return name;}@OneToOne    //設定關聯關係@PrimaryKeyJoinColumn(name="wifeId")            //@PrimaryKeyJoinColumn用主鍵來做串連,指定資料庫中的名字,預設為wife_idpublic Wife getWife() {return wife;}public void setId(Integer id) {this.id = id;}public void setName(String name) {this.name = name;}public void setWife(Wife wife) {this.wife = wife;}}

@PrimaryKeyJoinColumn 設定用主鍵來做串連


2、寫Wife.java類

package com.hibernate._0800_one2one_uni_pk;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.OneToOne;@Entitypublic class Wife {private Integer id;private String name;private Husband husband;/** mappedBy="wife"中的這個wife是Husband中的wife屬性,表示Husband起主導作用如果不加,在資料庫中Wife表中會產生husband_id這個冗餘欄位*/@OneToOne(mappedBy="wife")              public Husband getHusband() {return husband;}@Id        @PrimaryKeyJoinColumn(name="husbandId")         //加上這個註解就變成一對一雙向主鍵關聯       public Integer getId() {return id;}public String getName() {return name;}public void setHusband(Husband husband) {this.husband = husband;}public void setId(Integer id) {this.id = id;}public void setName(String name) {this.name = name;}}

設定mappedBy屬性,mappedBy="wife"中的這個wife是Husband中的wife屬性,表示Husband起主導作用如果不加,在資料庫中Wife表中會產生husband_id這個冗餘欄位

                                                                                                                                                                                               
                                         

3、XML中的配置。

單向主鍵關聯時,

Husband.hbm.xml檔案中配置如下:

<id name="id"><generator class="foreign"><param name="property">wife</param>   <!-- 這個值wife --></generator>      <!--不寫貌似也沒關係,但最好還是class="" 值最好設為foreign--></id><property name="name" /><one-to-one name="wife" constrained="true"/>   <!-- 加上constrained="true"來設定外鍵約束 -->

Wife.hbm.xml 中的配置如下;

<id name="id">      <generator class="native"></generator></id><property name="name" />

雙向主鍵關聯時,

Husband.hbm.xml檔案中配置如下:

<id name="id"><generator class="native"></generator></id><property name="name" /><one-to-one name="wife" property-ref="husband"/>

Wife.hbm.xml 中的配置如下;

<id name="id"><generator class="native"></generator></id><property name="name" /><one-to-one name="husband" constrained="true"/> 

聯繫我們

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