Hibernate映射解析——關聯映射(四)

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   os   sp   for   strong   

下面為大家介紹一對一關聯映射的第二種實現方式,即唯一外部索引鍵關聯。


唯一外部索引鍵關聯:外部索引鍵關聯,本來是用於多對一的配置,但是如果加上唯一的限制之後,也可以用來表示一對一關聯關係。


唯一外部索引鍵關聯也採用兩種方式,即單向與雙向。物件模型都與主鍵關聯的單雙向一致,不同的是形成的關係模式。


單向唯一外鍵:

採用<many-to-one>標籤來映射,指定多的一端unique為true,這樣就限制了多的一端的多重性唯一。


關聯式模式: 

  


IdCard.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><class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"><id name="id"><generator class="native"/></id><property name="cardNo"/></class></hibernate-mapping>

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><class name="com.bjpowernode.hibernate.Person" table="t_person"><id name="id"><generator class="native"/></id><property name="name"/><many-to-one name="idCard" unique="true"/></class></hibernate-mapping>


雙向唯一外鍵:


雙向唯一外部索引鍵關聯採用<one-to-one>標籤映射,必須制定<one-to-one>標籤中的property-ref屬性為關係欄位的名稱


IdCard.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><class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"><id name="id"><generator class="native"/></id><property name="cardNo"/><one-to-one name="person" property-ref="idCard"/></class></hibernate-mapping>

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><class name="com.bjpowernode.hibernate.Person" table="t_person"><id name="id"><generator class="native"/></id><property name="name"/><many-to-one name="idCard" unique="true"/></class></hibernate-mapping>


!!註:雙向唯一外鍵的關聯式模式與單向模型一致,還是由person來維護,只是物件模型變成了雙向箭頭,互相持有對象的引用。


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.