1. 程式人生 > >Hibernate中的表的多對多關係及操作

Hibernate中的表的多對多關係及操作

表的多對多關係及操作

多對多的表關係表達


表:
使用中間表,分別引用兩方的ID
物件: 
兩方都使用集合表達
配置:
<set name="courses" table="t_student_course"  >
<key column="sid" ></key>
<many-to-many class="Course" column="cid" ></many-to-many>
</set>

操作:
inverse: 我是否要放棄維護外來鍵關係
cascade: 是否需要級聯操作 (5個)
注意: 配置級聯刪除時,要小心,雙方都配置級聯刪除, 任意刪除一條記錄, 整個關係鏈資料都會被刪除.


多對多的物件關係表達

配置:

Student:
package com.itheima.domain;

import java.util.HashSet;
import java.util.Set;

public class Student {
	private Integer id;
	private String name;
	
	private Set<Course> courses = new HashSet<Course>();

	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 Set<Course> getCourses() {
		return courses;
	}

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}


}


Course:
package com.itheima.domain;

import java.util.HashSet;
import java.util.Set;

public class Course {
	private Integer id;
	private String name;
	
	private Set<Student> students = new HashSet<Student>();

	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 Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

}


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">
    <!-- ORM元資料  表物件關係對映檔案 
    	package : 配置該配置檔案中類所在的包.  -->
 <hibernate-mapping package="com.itheima.domain" >
 	<class name="Student" table="t_student"   >
		<id name="id" column="id"    >
			<generator class="native"></generator>
		</id> 	
		<property name="name" column="name" ></property>
		
		<!-- 多對多關係 -->
		<!-- 
			set 表達集合
				name: 集合的屬性名
				table:多對多中間表的表名
			key 表達外來鍵
				column:引用我的外來鍵名
			many-to-many 表達多對多
				class : 集合引用方的型別
				column:對方在中間表的外來鍵名
		 -->
		<set name="courses" table="t_student_course" inverse="false" cascade="save-update"  >
			<key column="sid" ></key>
			<many-to-many class="Course" column="cid" ></many-to-many>
		</set>
	
 	</class>
 </hibernate-mapping>


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">
    <!-- ORM元資料  表物件關係對映檔案 
    	package : 配置該配置檔案中類所在的包.  -->
 <hibernate-mapping package="com.itheima.domain" >
 	<class name="Course" table="t_course"  >
		<id name="id" column="id"    >
			<generator class="native"></generator>
		</id> 	
		<property name="name" column="name" ></property>
		
		<set name="students" table="t_student_course" inverse="true" >
			<key column="cid" ></key>
			<many-to-many class="Student" column="sid"  ></many-to-many>
		</set>
 	</class>
 </hibernate-mapping>


操作:

package com.itheima.a_manytomany;

import org.hibernate.Session;
import org.junit.Test;

import com.itheima.domain.Course;
import com.itheima.domain.Student;
import com.itheima.utils.HibernateUtils;

public class Demo {
	@Test
	//儲存學生 => 通過學生儲存課程. 由學生維護外來鍵
	//Student 的
		//inverse =  false
		//cascade =  save-update
	//Course
		//inverse = true
	public void fun1(){
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		//------------------------------------------------
		Student stu1 = new Student();
		stu1.setName("tom");
		
		Student stu2 = new Student();
		stu2.setName("jerry");
		
		Course c1 = new Course();
		c1.setName("Struts2");
		
		Course c2 = new Course();
		c2.setName("Hibernate");
		
		Course c3 = new Course();
		c3.setName("Spring");
		
		stu1.getCourses().add(c1); //維護關係,級聯儲存
		stu1.getCourses().add(c2);
		stu1.getCourses().add(c3);

		stu2.getCourses().add(c1);
		stu2.getCourses().add(c2);
		stu2.getCourses().add(c3);
		
		session.save(stu1);
		session.save(stu2);
		//------------------------------------------------
		session.getTransaction().commit();
		session.close(); // 遊離狀態
	}
	
}