hibernate 第七章 映射一对一 多对多关联关系

来源:互联网 发布:程序员兼职 知乎 编辑:程序博客网 时间:2024/06/01 10:50

student

package cn.itcast.hibernate.many2many;import java.util.HashSet;import java.util.Set;public class Student {private Integer id;private String name;private Set<Course> courses = new HashSet<Course>(0);public Student(){}public Student(String name) {this.name = name;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Course> getCourses() {return courses;}public void setCourses(Set<Course> courses) {this.courses = courses;}}

Course

package cn.itcast.hibernate.many2many;import java.util.HashSet;import java.util.Set;public class Course {private Integer id;private String name;private Set<Student> students = new HashSet<Student>(0);public Course(){}public Course(String name) {this.name = name;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}}

student.hbm.xml

package cn.itcast.hibernate.many2many;import java.util.HashSet;import java.util.Set;public class Course {private Integer id;private String name;private Set<Student> students = new HashSet<Student>(0);public Course(){}public Course(String name) {this.name = name;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}}

Course.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="cn.itcast.hibernate.many2many.Course" table="courses"><id name="id" type="integer"><column name="id"></column><generator class="increment"></generator></id><property name="name" type="string"><column name="name"></column></property><set name="students" table="student_course"><key><column name="cid"></column></key><many-to-many class="cn.itcast.hibernate.many2many.Student" column="sid"></many-to-many></set></class></hibernate-mapping>

APP测试类

package cn.itcast.hibernate.many2many;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;public class app {private static SessionFactory sf=null;static{Configuration config=new Configuration();config.configure("cn/itcast/hibernate/many2many/hibernate.cfg.xml");config.addClass(Student.class);config.addClass(Course.class);sf=config.buildSessionFactory();}//测试保存@Testpublic void testSave(){Session s=sf.openSession();Transaction ts=s.beginTransaction();Student s1=new Student("关羽");Student s2=new Student("张飞");Course c1=new Course("java");Course c2=new Course("android");s1.getCourses().add(c1);s1.getCourses().add(c2);s2.getCourses().add(c1);s2.getCourses().add(c2);c1.getStudents().add(s1);c1.getStudents().add(s2);c2.getStudents().add(s2);c2.getStudents().add(s1);s.save(c1);s.save(c2);s.save(s1);s.save(s2);ts.commit();s.close();}// 解除1号学生和1号课程之间的关系@Testpublic void testRemoveManyToMany(){Session s=sf.openSession();Transaction ts=s.beginTransaction();//查询1号学生 从course集合中删除Student stu=(Student)s.get(Student.class,1);//查询1号课程 从students集合中删除Course cour=(Course)s.get(Course.class,1);stu.getCourses().remove(cour);cour.getStudents().remove(stu);ts.commit();s.close();}//改变1号学生和2号课程的关系 该1号学生和1号课程@Testpublic void changeManytoMany(){Session s=sf.openSession();Transaction ts=s.beginTransaction();//先解除1号和2号的关系 在建立关系Student stu1=(Student)s.get(Student.class,1);Course c2=(Course)s.get(Course.class,2);Course c1=(Course)s.get(Course.class,1);stu1.getCourses().remove(c2);c2.getStudents().remove(stu1);stu1.getCourses().add(c1);c1.getStudents().add(stu1);ts.commit();s.close();}//知识点7:删除2号学生,中间引用不能删除@Testpublic void removeStudent(){Session s=sf.openSession();Transaction tx=s.beginTransaction();//查询1号学生 Student s2=(Student)s.get(Student.class, 2); s.delete(s2);          tx.commit();s.close();}//知识点8:删除1号课程,能删除 因为课程端是主控方,不用配置级联/* * 主控方,只能处理到主控方的表和中间表,不能处理其他的表 *       级联可以处理关联的表. */@Testpublic void removeCourse(){Session s=sf.openSession();Transaction tx=s.beginTransaction(); Course c1=(Course)s.get(Course.class, 1); s.delete(c1);     tx.commit();s.close();}//知识点9:删除1号课程的同时,要把1号和2号学生删掉?//在课程hbm。xml中配置级联删除  cascade="delete">@Testpublic void removeCourseCascade(){Session s=sf.openSession();Transaction tx=s.beginTransaction(); Course c1=(Course)s.get(Course.class, 1); s.delete(c1);     tx.commit();s.close();}}


原创粉丝点击