Hibernate映射数据 - 多对多映射

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/R812656252/article/details/84451526

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即是主键又是外键,在单向多对多映射中,上图只会存在一个主键,另一个则为外键。

猜你喜欢

转载自blog.csdn.net/R812656252/article/details/84451526