一、项目结构如下图
二、保存学生和课程及其学生选课关系代码如下(测试类中不能再有双向关联,否则会报错,因为,都维护了中间表外键,会有中间表外键冲突,如果非要写双向关联,就需要配置中设置某一方维护主键,课程和学生映射配置文件中相应配置inverse为true和false)
package com.hjp.manytomany;import java.util.HashSet;import java.util.Set;/** * Created by JiaPeng on 2016/1/2. */public class Course { private int id; private String name; public SetgetStudentSet() { return studentSet; } public void setStudentSet(Set studentSet) { this.studentSet = studentSet; } private Set studentSet = new HashSet (); @Override public String toString() { return "Course{" + "id=" + id + ", name='" + name + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
package com.hjp.manytomany;import java.util.HashSet;import java.util.Set;/** * Created by JiaPeng on 2016/1/2. */public class Student { private int id; private String name; private SetcourseSet = new HashSet (); @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", courseSet=" + courseSet + '}'; } public Set getCourseSet() { return courseSet; } public void setCourseSet(Set courseSet) { this.courseSet = courseSet; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/h1 root hjp123 org.hibernate.connection.C3P0ConnectionProvider 5 20 120 3000 true true create
public void func3() { Session session = HibernateUtils.getSession(); session.beginTransaction();//创建两个学生 Student student1 = new Student(); student1.setName("张三"); Student student2 = new Student(); student2.setName("李四"); //创建三门课程 Course course1 = new Course(); course1.setName("Java"); Course course2 = new Course(); course2.setName(".Net"); Course course3 = new Course(); course3.setName("php"); //学生关联课程 //张三java\php student1.getCourseSet().add(course1); student1.getCourseSet().add(course3); //李四php\.Net student2.getCourseSet().add(course2); student2.getCourseSet().add(course3); //课程关联学生// course1.getStudentSet().add(student1);// course2.getStudentSet().add(student2);// course3.getStudentSet().add(student1);// course3.getStudentSet().add(student2); session.save(student1); session.save(student2); session.save(course1); session.save(course2); session.save(course3); session.getTransaction().commit(); session.close(); }
三、解除id为1的学生和id为1的课程之间关系代码
public void func4(){ Session session = HibernateUtils.getSession(); session.beginTransaction(); //得到id为1的学生 Student student= (Student) session.get(Student.class,1); //得到id为1的课程 Course course= (Course) session.get(Course.class,1); //解除学生和课程的关系 student.getCourseSet().remove(course); course.getStudentSet().remove(student); session.getTransaction().commit(); session.close(); }
结果为中间表中s_id和c_id为1的记录被删除
上述代码中也可以只用course.getStudentSet().remove(student);代码删除,因为此时维护主键的一方是课程(course.hbm.xml中<set name="studentSet" table="s_c" inverse="false">和student.hbm.xml中<set name="courseSet" table="s_c" inverse="true">)
四、恢复id为1的学生和id为1的课程关系,并解除id为1的学生和id为3的课程间关系,单向绑定,利用中间表外键由课程维护
public void func5(){ Session session = HibernateUtils.getSession(); session.beginTransaction(); Student student= (Student) session.get(Student.class,1); Course course1= (Course) session.get(Course.class,1); Course course2= (Course) session.get(Course.class,3); course2.getStudentSet().remove(student); course1.getStudentSet().add(student); session.getTransaction().commit(); session.close(); }
五、删除id为1的学生,出现结果是id为1的学生被删了,而且中间表s_id为1的记录也删除了(上述代码中任然还是课程在维护中间表外键,执行下面代码要把双方inverse配置删除)
public void func6(){ Session session=HibernateUtils.getSession(); session.beginTransaction(); Student student= (Student) session.get(Student.class,1); session.delete(student); session.getTransaction().commit(); session.close(); }
六、在set节点设置cascade为delete或all-delete-orphan完成级联删除
比如id为1的学生有id为1和id为3课程,删除id为1的学生,不但中间表会删除相关记录,也会将id为1和id为3的课程删除