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

Hibernate對映資料 - 多對多對映

Hibernate對映資料 - 多對多對映

Hibernate在對映多對多的資料模型時,會採用中間表的形式,通過與中間表形成兩個一對多連線得到多對多的對映關係。


雙向多對多對映

學生資訊與課程資訊之間存在多對多的關係。本例中實現查詢學生下的所有課程資訊同時實現查詢某一課程下所有學生資訊。

1. Student.java

package com.java1234.hibernate.model;

import java.util.Set;

public class Student {

	private int id;
	private String name;
	private Set<Course> course_;
	
	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;
	}
	public Set<Course> getCourse_() {
		return course_;
	}
	public void setCourse_(Set<Course> course) {
		this.course_ = course;
	}
	
}

2. Course.java

package com.java1234.hibernate.model;

import java.util.Set;

public class Course {

	private int id;
	private String name;
	private Set<Student> student_;
	
	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;
	}
	public Set<Student> getStudent_() {
		return student_;
	}
	public void setStudent_(Set<Student> student_) {
		this.student_ = student_;
	}
}

3. student.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"/>
    
    	<!-- 通過中間表student_course來對映多對多關係 -->
    	<set name="course_" table="student_course">
    		<!-- 在Student的角度上 studentId 是student_course表的PK-->
    		<key column="studentId"/>
    		<!-- 在Student的角度上 courseId 是student_course表的FK,通過courseId連線Course表-->
    		<many-to-many class="com.java1234.hibernate.model.Course" column="courseId"/>
    	</set>
    	</class>
    </hibernate-mapping>

4. course.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.Course" table="tb_course">
    		<id name="id" column="courseId">
    			<generator class="native"/>
    		</id>
    		
    	<property name="name" column="courseName"/>
    	
    	<set name="student_" table="student_course">
    		<!-- 在Course的角度上 courseId 是student_course表的PK-->
    		<key column="courseId"/>
    		<!-- 在Course的角度上 studentId 是student_course表的FK,通過studentId連線Student表
    			最後courseId,studentId同時作為主鍵以及外來鍵-->
    		<many-to-many class="com.java1234.hibernate.model.Student" column="studentId"/>
    	</set>
    	</class>
    </hibernate-mapping>

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

生成的SQL

Hibernate: create table student_course (studentId integer not null, courseId integer not null, primary key (courseId, studentId))
Hibernate: create table tb_course (courseId integer not null auto_increment, courseName varchar(255), primary key (courseId))
Hibernate: create table tb_student (stuId integer not null auto_increment, name varchar(255), primary key (stuId))
Hibernate: alter table student_course add constraint FK_t45vpfbqbh56bh3u7d4ppoopg foreign key (courseId) references tb_course (courseId)
Hibernate: alter table student_course add constraint FK_pt5bwh1wp3fn0d5cr677p211f foreign key (studentId) references tb_student (stuId)

單向多對多對映與雙向多對多對映:如上的表結構:studnetId&courseId即是主鍵又是外來鍵,在單向多對多對映中,上圖只會存在一個主鍵,另一個則為外來鍵。