一、 多对多
***********************************************************************************
类:
public class Course implements Serializable {
private Long cid;
private Set<Student> students;
// set/get属性
}
public class Student implements Serializable {
private Long sid;
private Set<Course> courses;
// set/get属性
}
----------------------------------------------------------------------------------
配置文件:
Course.hbm.xml:
<hibernate-mapping>
<class name="cn.itcast.manyToMany.domain.Course">
<id name="cid" length="5">
<generator class="increment"></generator>
</id>
<!-- table就是用来描述第三张表的 -->
<set name="Students" table="student_course" cascade="save-update">
<key>
<column name="cid"></column>
</key>
<many-to-many class="cn.itcast.manyToMany.domain.Student" column="sid"></many-to-many>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml:
<hibernate-mapping>
<class name="cn.itcast.manyToMany.domain.Student" >
<id name="sid" type="java.lang.Long">
<generator class="increment"></generator>
</id>
<!-- table就是用来描述第三张表的 -->
<set name="courses" table="student_course" cascade="save-update">
<key>
<column name="sid"></column> <!-- 注: 此处在哪个映射文件中写哪个外键 -->
</key>
<many-to-many class="cn.itcast.manyToMany.domain.Course" column="cid"></many-to-many>
</set>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------
***********************************************************************************
例:
***********************************************************************************
public class ManyToManyTest {
private static String url;
static{
// 此处存放hibernate.cfg.xml文件路径
url = "cn/itcast/manyToMany/domain/hibernate.cfg.xml";
}
// 3、 新建课程的同时新建学生 级联
@Test
public void testSave_Student_Cascade_Course_Save(){
SessionFactory sessionFactory = HibernateUtils.getSessionFactoryByUrl(url);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = new Course();
course.setCname("java");
course.setDescription("面向对象编程");
Student student = new Student();
student.setSname("班长11111");
student.setDescription("牛人11111");
Set<Course> courses = new HashSet<Course>();
courses.add(course);
student.setCourses(courses);
session.save(student);
transaction.commit();
session.close();
}
// 4.1、 已经存在一个课程,新建一个学生,建立课程与学生之间的关系 课程角度发起
@Test
public void testUpdateCourse_Cascade_Student_Save_R(){
SessionFactory sessionFactory = HibernateUtils.getSessionFactoryByUrl(url);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = (Course) session.get(Course.class, 1L);
Student student = new Student();
student.setSname("班丝");
student.setDescription("班长的粉丝");
course.getStudents().add(student);
transaction.commit();
session.close();
}
// 4.2、 已经存在一个课程,新建一个学生,建立课程与学生之间的关系 学生角度发起
@Test
public void testSaveStudent_R(){
SessionFactory sessionFactory = HibernateUtils.getSessionFactoryByUrl(url);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = (Course) session.get(Course.class, 1L);
Student student = new Student();
student.setSname("班丝2");
student.setDescription("班长的粉丝2");
Set<Course> courses = new HashSet<Course>();
courses.add(course);
student.setCourses(courses);
session.save(student);
transaction.commit();
session.close();
}
// 6、 已经存在一个课程,已经存在一个学生,建立关联
@Test
public void testR(){
SessionFactory sessionFactory = HibernateUtils.getSessionFactoryByUrl(url);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = (Course) session.get(Course.class, 1L);
Student student = (Student) session.get(Student.class, 2L);
course.getStudents().add(student);
transaction.commit();
session.close();
}
// 7、 把已经存在的一些学生加入到已经存在的一个课程中
@Test
public void testR_Some(){
SessionFactory sessionFactory = HibernateUtils.getSessionFactoryByUrl(url);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = (Course) session.get(Course.class, 1L);
Student student2 = (Student) session.get(Student.class, 2L);
Student student3 = (Student) session.get(Student.class, 3L);
course.getStudents().add(student2);
course.getStudents().add(student3);
transaction.commit();
session.close();
}
// 8、 把一个学生加入到一些课程中
@Test
public void testR_Some_2(){
SessionFactory sessionFactory = HibernateUtils.getSessionFactoryByUrl(url);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = (Student) session.get(Student.class, 2L);
List<Course> courseList = session.createQuery("from Course where cid in(1,2,3)").list();
student.getCourses().addAll(courseList);
transaction.commit();
session.close();
}
// 12、把一个学生从一个课程转向到另外一个课程中
@Test
public void testTransform(){
SessionFactory sessionFactory = HibernateUtils.getSessionFactoryByUrl(url);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = (Student) session.get(Student.class, 1L);
Course course1 = (Course) session.get(Course.class, 1L);
Course course3 = (Course) session.get(Course.class, 3L);
student.getCourses().remove(course1);
student.getCourses().add(course3);
transaction.commit();
session.close();
}
// 14、删除学生
@Test
public void testDelStudent(){
SessionFactory sessionFactory = HibernateUtils.getSessionFactoryByUrl(url);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course1 = (Course) session.get(Course.class, 1L);
Set<Student> students = course1.getStudents();
// 注: 删除学生前,要先解除学生与课程的关系,且必须为同一发起对象来建立关系并解除
course1.setStudents(null);
for(Student student : students){
session.delete(student);
}
transaction.commit();
session.close();
}
}
***********************************************************************************