1,编写实体类
学生类
package org.ccit.com.domain;
import java.util.HashSet;
import java.util.Set;
/**
* @program: Hibernate_01
* @description
* @author: LIANG
* @create: 2021-03-04 17:05
**/
public class Student {
private Integer sid;//学生id
private String name;//学生名称
//学生对应的课程集合
Set<Courses> courses=new HashSet<>();
public Student() {
}
public Student(String name) {
this.name = name;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Courses> getCourses() {
return courses;
}
public void setCourses(Set<Courses> courses) {
this.courses = courses;
}
}
课程类
package org.ccit.com.domain;
import java.util.HashSet;
import java.util.Set;
/**
* @program: Hibernate_01
* @description
* @author: LIANG
* @create: 2021-03-04 17:06
**/
public class Courses {
private Integer cid; //课程
private String name; //课程姓名
//课程对应的学生集合
private Set<Student> student= new HashSet<>();
public Courses() {
}
public Courses(String name) {
this.name = name;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
}
编写映射文件
1,编写student的映射文件 对课程属性配置级联保存 关联学生课程后 只需保存student
相反在编写courses的映射文件时 对student属性配置级联保存 关联课程后 只需保存course
2,对于外键的维护 保存stuent 便使用student维护外键 编写student的映射文件 对课程属性 设置inverse=“false”
3,多对多表结构 inverse不能两边都为true,如果两边都为true 中间表将不会有数据
Courses.hbm.xml
<!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="org.ccit.com.domain.Courses" table="t_courses">
<id name="cid" column="cid">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="student" table="t_student_course" inverse="false" cascade="all-delete-orphan">
<!-- column填该表的主键-->
<key column="cid"></key>
<!-- column填属性类表主键-->
<many-to-many class="org.ccit.com.domain.Student" column="sid"></many-to-many>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.ccit.com.domain">
<class name="Student" table="t_student">
<id name="sid" column="sid">
<generator class="native"></generator>
</id>
<property name="name" column="name" length="20"></property>
<set name="courses" table="t_student_course" cascade="all-delete-orphan" inverse="true">
<key column="sid"></key>
<many-to-many class="Courses" column="cid"></many-to-many>
</set>
</class>
</hibernate-mapping>
编写测试类
import org.ccit.com.domain.Courses;
import org.ccit.com.domain.Student;
import org.ccit.com.uitls.HibernateUtils;
import org.hibernate.Session;
import org.junit.Test;
/**
* @program: Hibernate_01
* @description
* @author: LIANG
* @create: 2021-03-04 17:20
**/
public class HibernateTset05 {
@Test
public void method01(){
Session session = HibernateUtils.openSession();
session.getTransaction().begin();
Student student1 = new Student("张三");
Student student2 = new Student("李四");
Courses courses1 = new Courses("软件体系结构");
Courses courses2 = new Courses("软件测试");
//以下保存方式 将student映射文件中course属性的的级联保存打开 并将外键维护交给student
// <set name="courses" table="t_student_course" cascade="all-delete-orphan" inverse="false">
// student1.getCourses().add(courses1);
// student1.getCourses().add(courses2);
//
// student2.getCourses().add(courses1);
// student2.getCourses().add(courses2);
//
// session.save(student1);
// session.save(student2);
//相反 将course映射文件中student属性的级联保存打开 并将外键维护交给course
courses1.getStudent().add(student1);
courses1.getStudent().add(student2);
courses2.getStudent().add(student1);
courses2.getStudent().add(student2);
session.save(courses1);
session.save(courses2);
session.getTransaction().commit();
session.close();
}
}