一、项目结构如下图
二、保存学生和课程及其学生选课关系代码如下(测试类中不能再有双向关联,否则会报错,因为,都维护了中间表外键,会有中间表外键冲突,如果非要写双向关联,就需要配置中设置某一方维护主键,课程和学生映射配置文件中相应配置inverse为true和false)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 Set<Student> getStudentSet() { return studentSet; } public void setStudentSet(Set<Student> studentSet) { this.studentSet = studentSet; } private Set<Student> studentSet = new HashSet<Student>(); @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; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hjp.manytomany.Course" table="course"> <id name="id"> <generator class="native"></generator> </id> <property name="name" length="20"></property> <set name="studentSet" table="s_c"> <key column="c_id"></key> <many-to-many class="com.hjp.manytomany.Student" column="s_id"></many-to-many> </set> </class> </hibernate-mapping>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 Set<Course> courseSet = new HashSet<Course>(); @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", courseSet=" + courseSet + '}'; } public Set<Course> getCourseSet() { return courseSet; } public void setCourseSet(Set<Course> 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; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hjp.manytomany.Student" table="student"> <id name="id"> <generator class="native"></generator> </id> <property name="name" length="20"></property> <set name="courseSet" table="s_c"> <key column="s_id"></key> <many-to-many class="com.hjp.manytomany.Course" column="c_id"></many-to-many> </set> </class> </hibernate-mapping>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/h1</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">hjp123</property> <!--设置c3p0--> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!--c3p0连接池配置信息--> <property name="c3p0.min_size">5</property> <property name="c3p0.max_size">20</property> <property name="c3p0.timeout">120</property> <property name="c3p0.idle_test_period">3000</property> <!--显示SQL并格式化--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!--ddl生成策略,后面可以改为update--> <property name="hibernate.hbm2ddl.auto">create</property> <!--导入映射文件--> <mapping resource="com/hjp/manytomany/course.hbm.xml"></mapping> <mapping resource="com/hjp/manytomany/student.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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的课程之间关系代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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的课程间关系,单向绑定,利用中间表外键由课程维护
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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配置删除)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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的课程删除