多对多表的搭建
java类中
多对多
1、关系操作
1、多对多,谁操作效率都一样
2、解除关系
把第三张表的一行数据删除掉
3、建立关系
把第三张表的数据增加一行记录
4、变更关系
先删除后增加
2、级联操作
都是对象针对集合的操作
例子
工具类
public class HibernateUtils { public static SessionFactory sessionFactory; public static String url; @Before public void init(){ Configuration configuration = new Configuration(); if(url==null){ configuration.configure(); }else{ configuration.configure(url); } sessionFactory = configuration.buildSessionFactory();}
student映射文件
courses映射文件
测试
@Testpublic void testCreateTable(){}@Testpublic void testSaveStudent_Cascade_Courses_Save(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = new Student(); student.setSname("班长"); student.setDescription("牛人"); Courses Courses = new Courses(); Courses.setCname("生理卫生"); Courses.setDescription("很好"); SetCoursess = new HashSet (); Coursess.add(Courses); student.setCoursess(Coursess); session.save(student); transaction.commit(); session.close();}/** * 已经存在一个课程,新建一个学生,建立课程和学生之间的关系 * 从课程角度出发 */@Testpublic void testUpdateCourses_Cascade_Student_Save_R(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Courses Courses = (Courses)session.get(Courses.class, 1L); Student student = new Student(); student.setSname("班迷"); student.setDescription("班丝:班长的钢丝"); Courses.getStudents().add(student); session.save(student); transaction.commit(); session.close();}/** * 已经存在一个课程,新建一个学生,建立课程和学生之间的关系 * 从学生角度出发 */@Testpublic void testSaveStudent_R(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Courses Courses = (Courses)session.get(Courses.class, 1L); Student student = new Student(); student.setSname("班迷"); student.setDescription("班丝:班长的钢丝"); Set Coursess = new HashSet (); Coursess.add(Courses); student.setCoursess(Coursess); session.save(student); transaction.commit(); session.close();}/** * 已经存在一个课程,已经存在一个学生,建立关联 */@Testpublic void testR(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Courses Courses = (Courses)session.get(Courses.class, 1L); Student student = (Student)session.get(Student.class, 2L); student.getCoursess().add(Courses); transaction.commit(); session.close();}/** * 把学生3,4加入到课程1中 */@Testpublic void testR_Some(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Courses Courses = (Courses)session.get(Courses.class, 1L); Student student = (Student)session.get(Student.class, 3L); Student student2 = (Student)session.get(Student.class, 4L); //student.getCoursess().add(Courses); //student2.getCoursess().add(Courses); Courses.getStudents().add(student2); Courses.getStudents().add(student); transaction.commit(); session.close();}/** * 把一个学生加入到一些课程中 */@Testpublic void testR_Some_2(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = (Student)session.get(Student.class, 3L); List CoursesList = session.createQuery("from Courses where cid in(1,2,3)").list(); student.getCoursess().addAll(CoursesList); transaction.commit(); session.close();}/** * 把学生从一个课程转移到另外一个课程 */@Testpublic void testTransform(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = (Student) session.get(Student.class, 3L); Courses Courses1 = (Courses)session.get(Courses.class, 1L); Courses Courses3 = (Courses)session.get(Courses.class, 3L); student.getCoursess().remove(Courses1); student.getCoursess().add(Courses3); transaction.commit(); session.close();}
关联删除的错误
1、根据映射文件可以得出classes与student有关联
2、在客户端,students是由classes产生的,代表了关联关系
3、所以在删除student的时候,必须通过classes解除关系
4、解除关系以后才能进行删除