Hibernate学习之级联操作4——多对多关联关系

这一篇记录的是多对多的关联关系,学生与课程的关系就是多对多的关系,数据库如下所示:

Hibernate学习之级联操作4——多对多关联关系_第1张图片

学生表中包含id和name,course表一样的,student_course表则包含学生id和课程id,这个表是连接学生与课程多对多关系的表,下面是学生和课程的两个实体类,这两个类中的成员变量如下图(省略getter和setter方法):

Hibernate学习之级联操作4——多对多关联关系_第2张图片

Hibernate学习之级联操作4——多对多关联关系_第3张图片

下面是Student.hbm.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.test.model.Student" table="student" select-before-update="true">
        <id name="id" unsaved-value="null" column="id">
            <generator class="uuid">
            </generator>
        </id>
        
        <property name="name" type="string">
        	<column name="name" length="20"></column>
        </property>
        
        <set name="courses" table="student_course" inverse="true" cascade="none">
        	<key column="stu_id"></key>
        	<many-to-many class="com.test.model.Course" column="cour_id"></many-to-many>
        </set>
        
    </class>
</hibernate-mapping>
需要注意的是set标签,里面加了一个table属性

下面是Course.hbm.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.test.model.Course" table="course" select-before-update="true">
        <id name="id" unsaved-value="null" column="id">
            <generator class="uuid">
            </generator>
        </id>
        
        <property name="name" type="string">
        	<column name="name" length="20"></column>
        </property>
        
        <set name="students" table="student_course" cascade="save-update">
        	<key column="cour_id"></key>
        	<many-to-many class="com.test.model.Student" column="stu_id"></many-to-many>
        </set>
        
    </class>
</hibernate-mapping>
下面是测试代码:

package com.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.test.model.Course;
import com.test.model.Student;

public class TestMain {

	public static void main(String[] args) {
		Student stu1 = new Student();
		stu1.setName("zhangsan");
		
		Course c1 = new Course();
		c1.setName("history");
		
		stu1.getCourses().add(c1);
		c1.getStudents().add(stu1);
		
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		try {
			session.save(c1);
			trans.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		
	}

}
在控制台中打印的结果为:

Hibernate学习之级联操作4——多对多关联关系_第4张图片
可以看到student和course、student_course表中各插入了一条数据


源代码下载


你可能感兴趣的:(Hibernate,多对多,关联关系)