直接演示代码:
Course类 (课程)
public class Course implements Serializable{
private Long cid;
private String name;
private String description;
private Set<Student> students;
Student类(学生)
public class Student implements Serializable{
private Long sid;
private String name;
private String description;
private Set<Course> courses;
Course.hbm.xml
<hibernate-mapping>
<class name="com.itheima12.hibernate.domain.Course">
<id name="cid" length="5">
<generator class="increment"></generator>
</id>
<property name="description" length="50"></property>
<property name="name" length="20"></property>
<!--
table
第三张表
-->
<set name="students" table="student_course" cascade="save-update">
<key>
<!--
外键
-->
<column name="cid"></column>
</key>
<!--
column外键
-->
<many-to-many class="com.itheima12.hibernate.domain.Student" column="sid"></many-to-many>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml
<hibernate-mapping>
<class name="com.itheima12.hibernate.domain.Course">
<id name="cid" length="5">
<generator class="increment"></generator>
</id>
<property name="description" length="50"></property>
<property name="name" length="20"></property>
<!--
table
第三张表
-->
<set name="students" table="student_course" cascade="save-update">
<key>
<!--
外键
-->
<column name="cid"></column>
</key>
<!--
column外键
-->
<many-to-many class="com.itheima12.hibernate.domain.Student" column="sid"></many-to-many>
</set>
</class>
</hibernate-mapping>
测试代码
public class ManyToManyTest extends HibernateUtils{
/**
* 保存学生的时候,同时保存课程
* Hibernate:
select
max(sid)
from
Student
Hibernate:
select
max(cid)
from
Course
Hibernate:
insert
into
Student
(description, name, sid)
values
(?, ?, ?)
Hibernate:
在Student.hbm.xml文件中
<set cascade="save-update">
insert
into
Course
(description, name, cid)
values
(?, ?, ?)
Hibernate:
建立关系的sql语句
insert
into
student_course
(sid, cid)
values
(?, ?)
*/
@Test
public void testSaveStudent_Cascade_Save_Course(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = new Student();
student.setName("aa");
Course course = new Course();
course.setName("java基础课程");
//建立学生与课程之间的关系
Set<Course> courses = new HashSet<Course>();
courses.add(course);
student.setCourses(courses);
session.save(student);
transaction.commit();
session.close();
}
/**
* 已经存在一个课程,已经存在一个学生,建立该课程和该学生之间的关系
*/
@Test
public void testBuildR(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
//把cid为2的课程和sid为1的学生提取出来
Course course = (Course)session.get(Course.class, 2L);
Student student = (Student)session.get(Student.class, 1L);
student.getCourses().add(course);
transaction.commit();
session.close();
}
/**
* 已经存在一个课程,新建一个学生,建立该课程和该学生之间的关系
*/
@Test
public void testSaveStudent_BuildR(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = new Student();
student.setName("student1");
Course course = (Course)session.get(Course.class, 1L);
/**
* 建立了新的学生与该课程之间的关系
*/
Set<Course> courses = new HashSet<Course>();
courses.add(course);
student.setCourses(courses);
session.save(student);
transaction.commit();
session.close();
}
/**
* 一个学生从一门课程转到另外一门课程
*/
@Test
public void testTranform(){
/**
* sid为1的学生从课程2转到课程1
*/
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
//提取sid为1的学生
Student student = (Student)session.get(Student.class, 1L);
//把课程1和课程2提取出来
Course course1 = (Course)session.get(Course.class, 1L);
Course course2 = (Course)session.get(Course.class, 2L);
student.getCourses().remove(course2);//解除sid为1的学生和cid为2的课程
student.getCourses().add(course1);//建立sid为1的学生和cid为1的课程
transaction.commit();
session.close();
}
/**
* 解除一个学生和该学生所学的所有的课程之间的关系
*/
@Test
public void testRealseAllR(){
/**
* 解除sid为1的学生和所有的课程之间的关系
*/
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = (Student)session.get(Student.class, 1L);
student.setCourses(null);
transaction.commit();
session.close();
}
/**
* 解除一个学生和所有的课程之间的关系,再建立一些课程之间的关系
*/
@Test
public void testRealseAllRAndBuildR(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = (Student)session.get(Student.class, 1L);
Set<Course> courses = new HashSet<Course>();
Course course = (Course)session.get(Course.class, 3L);
courses.add(course);
//把原来的student中的courses给覆盖掉了
student.setCourses(courses);
transaction.commit();
session.close();
}