1. 程式人生 > >Hibernate對映資料 - 一對一對映

Hibernate對映資料 - 一對一對映

Hibernate對映資料 - 一對一對映

Hierbnate中生成一對一對映關係的策略有主鍵對映以及外來鍵對映兩種。
主鍵對映: 通過將兩個相關聯的表生成共享主鍵,完成一對一的資料庫表結構。
外來鍵對映: 會在一個表中生成用於連結另一個表的外來鍵,再通過unique屬性實現一對一的資料庫表結構。

例子:生成一個地址表(tb_address)用於儲存學生的唯一的位置資訊。

主鍵對映(雙向)

1. student.java

package com.java1234.hibernate.model;

public class Student {

	private int id;
	private String name;
	private Address address;
	
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

2. address.java

package com.java1234.hibernate.model;

public class Address {

	private int id;
	private String zipCode;
	private String address;
	private Student student;
	
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	
}

3. stduent.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="com.java1234.hibernate.model.Student" table="tb_student">
    		<id name="id" column="stuId">
    			<generator class="native"/>
    		</id>
    		<property name="name" column="stuName"/>
    		
   			<!-- 新增一對一對映,當查詢學生對應的地址資訊時,通過以下屬性完成對映 -->
   			<one-to-one name="address" class="com.java1234.hibernate.model.Address"/>
    	</class>
    
    </hibernate-mapping>

4. address.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="com.java1234.hibernate.model.Address" table="tb_address">
    		<id name="id" column="addId">
    		<!-- 定義主鍵生成策略為foreign,通過property繫結Address中的物件,
    			使得Address與Stduent的物件擁有統一主鍵 -->
    			<generator class="foregin">
    				<param name="property">student</param>
    			</generator>
    		</id>
    		<property name="zipCode" column="zipcode"/>
    		<property name="address" column="address"/>
    		<!-- 新增一對一對映,當查詢地址對應的學生資訊時,通過以下屬性完成對映,
    			並用 constrained完成共享主鍵約束 -->
    		<one-to-one name="student" class="com.java1234.hibernate.model.Student" constrained="true"/>
    	</class>
    
    </hibernate-mapping>

5. 生成的表結構/所執行的SQL

執行的SQL

Hibernate: create table tb_address (addId integer not null, zipcode varchar(255), address varchar(255), primary key (addId))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), primary key (stuId))
Hibernate: alter table tb_address add constraint FK_bssu6l647bdlxvhi84i6c0p8x foreign key (addId) references tb_student (stuId)

 共享主鍵: stuId=1,addId=1 =>兩個表的資料是相互順序對應的。
6. 測試插入資料

TBD - 2018/11/20

 

外來鍵連線(單向/雙向)

1. student.java
2. address.java

      同上
3. stduent.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="com.java1234.hibernate.model.Student" table="tb_student">
    		<id name="id" column="stuId">
    			<generator class="native"/>
    		</id>
    		<property name="name" column="stuName"/>
    		
    		<!-- 使用多對一對映,在tb_student中宣告外來鍵,並且使用unique="true" 說明外來鍵只對應唯一的一個 -->
   			<many-to-one name="address" class="com.java1234.hibernate.model.Address" 
   						column="addressId" cascade="all" unique="true"/>
    	</class>
    
    </hibernate-mapping>

4. address.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="com.java1234.hibernate.model.Address" table="tb_address">
    		<id name="id" column="addressId">
    			<generator class="native"/>
    		</id>
    		<property name="zipCode" column="zipcode"/>
    		<property name="address" column="address"/>
    		
    		<!-- 無法通過addressId查到對應的學生資訊-->
    		<one-to-one name="student" class="com.java1234.hibernate.model.Student"/> 
    		
    		<!-- 通過 property-ref=student 能過通過addressId反向對映到student資訊,address是student.java中的屬性名
    		<one-to-one name="student" class="com.java1234.hibernate.model.Student" property-ref="address" />
    		-->	
    	</class>
    
    </hibernate-mapping>

5. 生成的表結構/所執行的SQL

SQLs:

Hibernate: create table tb_address (addressId integer not null auto_increment, zipcode varchar(255), address varchar(255), primary key (addressId))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), addressId integer, primary key (stuId))
Hibernate: alter table tb_student add constraint UK_3b3r83kx7qks14wojkp07mu10  unique (addressId)
Hibernate: alter table tb_student add constraint FK_3b3r83kx7qks14wojkp07mu10 foreign key (addressId) references tb_address (addressId)