(十)Hibernate的一對一關聯關系
阿新 • • 發佈:2018-08-10
app 不變 配置 class a tor com get class etc
一、概述
我們以Company與Address類為例,介紹映射一對一關聯關系的方法。
一對一的關聯方法有兩種
-按照外鍵映射:兩個表任意一個表定義一個外鍵,來關聯另一個表。
-按照主鍵映射:一個表的主鍵同時作為外鍵,和另一個表的主鍵保持一致。
二、按照外鍵映射
(1)創建實體類
public class Company { private Integer id; private String name; private Address address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } } public class Address { private Integer id; private String name; private Company company; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Company getCompany() { return company; } public void setCompany(Company company) { this.company = company; } }
(2)我們配置映射文件,我們在Company這一方設置外鍵來關聯Address 有外鍵的一方要使用<many-to-one>元素來配置。
Company.hbm.xml <hibernate-mapping > <class name="com.cad.domain.Company" table="company"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <!--column指定外鍵,unique指定外鍵唯一約束,設為true,就可以表達Company和Address對象之間的一對一關聯--> <many-to-one name="address" class="com.cad.domain.Address" column="aid" unique="true"></many-to-one> </class> </hibernate-mapping> Address.hbm.xml <hibernate-mapping > <class name="com.cad.domain.Address" table="address"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <!--property-ref屬性指定通過從Company的address屬性來查找自己--> <one-to-one name="Company" class="com.cad.domain.Company" property-ref="address"></one-to-one> </class> </hibernate-mapping>
測試:
public class Demo { private Session session; @Test public void test() { //讀取配置文件 Configuration conf=new Configuration().configure(); //根據配置創建factory SessionFactory sessionfactory=conf.buildSessionFactory(); session = sessionfactory.openSession(); Transaction ts=session.beginTransaction(); Company c=new Company(); c.setName("百度"); Address a=new Address(); a.setName("深圳"); c.setAddress(a); a.setCompany(c); session.save(a); session.save(c); ts.commit(); session.close(); sessionfactory.close(); } } 默認情況下,一對一關聯采用迫切左外連接檢索策略。
三、按照主鍵映射
實體類不變,我們編寫一下配置文件 address的表的id字段既是主鍵又是外鍵. 編寫配置文件 Company.hbm.xml <hibernate-mapping > <class name="com.cad.domain.Company" table="company"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <one-to-one name="address" class="com.cad.domain.Address" ></one-to-one> </class> </hibernate-mapping> Address.hbm.xml <hibernate-mapping > <class name="com.cad.domain.Address" table="address"> <id name="id" column="id"> <!--必須使用foreign標識符生成策略,還要指定哪個對象共享OID--> <generator class="foreign"> <param name="property">company</param> </generator> </id> <property name="name" column="name"></property> <!--constrained屬性設為true,說明主鍵同時作為外鍵--> <one-to-one name="company" class="com.cad.domain.Company" constrained="true"></one-to-one> </class> </hibernate-mapping>
(十)Hibernate的一對一關聯關系