一:多对多映射概念
1.实体多对多关联
多对多定义(1:n):
如果对于实体集A中的每一个实体,实体集B中有n个实体(n>=0)与之联系,反之,对于实体集B中的每一个实体,实体集A中也有m个实体(m>=0)与之联系,则称为实体集A与实体集B具有多对多联系,记为m:n。
示例:一个人可以选修多门课程,一门课程可以被多名同学选修
示例:
2.数据库多对多关联
多增加一张表来表示学生和课程之间的关系。
二:Hibernate实现多对多关联映射
1.使用xml的方式实现
Course类:
public class Course { private int id; private String name; private String courseNo; private Set<Student> students = new HashSet<>(); 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; } public String getCourseNo() { return courseNo; } public void setCourseNo(String courseNo) { this.courseNo = courseNo; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } @Override public String toString() { return "Course [id=" + id + ", name=" + name + ", courseNo=" + courseNo + ", students=" + students + "]"; } }
Course类配置文件:
<?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 package="com.hibernate.entity"> <class name="Course" table="course"> <id name="id" type="int"> <column name="id"></column> <generator class="native"></generator> </id> <property name="name" type="java.lang.String"> </property> <property name="courseNo" column="course_no" type="java.lang.String" /> <set name="students" table="student_course" inverse="true" cascade="all"> <key column="course_id"></key> <many-to-many class="Student" column="student_id"> </many-to-many> </set> </class> </hibernate-mapping>
Student类:
public class Student { private int id; private String name; private String studentNo; private Set<Course> courses = new HashSet<>(); 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; } public String getStudentNo() { return studentNo; } public void setStudentNo(String studentNo) { this.studentNo = studentNo; } public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", studentNo=" + studentNo + ", courses=" + courses + "]"; } }
Student类配置文件:
<?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 package="com.hibernate.entity"> <class name="Student" table="student"> <id name="id" type="int"> <column name="id"></column> <generator class="native"></generator> </id> <property name="name" type="java.lang.String"> </property> <property name="studentNo" column="stu_no" type="java.lang.String" /> <set name="courses" table="student_course" inverse="false" cascade="all"> <key column="student_id"></key> <many-to-many class="Course" column="course_id"> </many-to-many> </set> </class> </hibernate-mapping>
测试类:
public class Test { public static void main(String[] args) { saveStudent(); HibernateUtil.closeFactory(); } public static void saveStudent() { // 1.获取session对象 Session session = HibernateUtil.openSession(); // 2.得到用户对象 Student student = new Student(); student.setName("张三"); student.setStudentNo("2018"); Set<Course> courses = new HashSet<>(); Course course = new Course(); course.setName("Java程序设计"); course.setCourseNo("2020"); courses.add(course); student.setCourses(courses); Transaction transaction = session.beginTransaction(); session.save(student); transaction.commit(); // 4.关闭session对象 session.close(); // 5.关闭sessionFactory } }
2.使用注解的方式
Course类:
@Entity @Table(name = "course") public class Course { private int id; private String name; private String courseNo; private Set<Student> students = new HashSet<>(); @Id @GeneratedValue(generator = "nat") @GenericGenerator(name = "nat", strategy = "native") 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; } @Column(name = "course_no") public String getCourseNo() { return courseNo; } public void setCourseNo(String courseNo) { this.courseNo = courseNo; } @ManyToMany(mappedBy = "courses", cascade = CascadeType.ALL) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } @Override public String toString() { return "Course [id=" + id + ", name=" + name + ", courseNo=" + courseNo + ", students=" + students + "]"; } }
Student类:
@Entity @Table(name = "student") public class Student { private int id; private String name; private String studentNo; private Set<Course> courses = new HashSet<>(); @Id @GeneratedValue(generator = "nat") @GenericGenerator(name = "nat", strategy = "native") 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; } @Column(name = "stu_no") public String getStudentNo() { return studentNo; } public void setStudentNo(String studentNo) { this.studentNo = studentNo; } @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "student_course", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id")) public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", studentNo=" + studentNo + ", courses=" + courses + "]"; } }