Hibernate关联关系映射—多对多详解编程语言

多对多
实例:学生–课程
实例分析:
这里写图片描述

代码实现:

Student.java

package com.my.bean; 
 
import java.util.HashSet; 
import java.util.Set; 
 
public class Student { 
 
    private Integer id; //native 
    private String name; 
 
    //不同的学生可以学习不同课程 (多对多) 
    // 当前学生可以学习【多个课程】 
    private Set<Course> courseSet = new HashSet<Course>(); 
 
    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> getCourseSet() { 
        return courseSet; 
    } 
 
    public void setCourseSet(Set<Course> courseSet) { 
        this.courseSet = courseSet; 
    } 
 
} 

Student.hbm.xml

<?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.my.bean.Student" table="t_student"> 
        <id name="id"> 
            <generator class="native"></generator> 
        </id> 
        <property name="name"></property> 
 
        <!--  
            不同的学生可以学习不同课程 (多对多),当前学生可以学习 
            1 使用集合 ## 从表表名 
            2 从表对应的外键名称 
            3 关联关系(多对多):类型,另一个外键名称 
         --> 
         <set name="courseSet" table="t_student_course" > <!-- cascade="save-update,delete" --> 
            <key column="student_id"></key> 
            <many-to-many class="com.my.bean.Course" column="course_id"></many-to-many> 
         </set> 
 
    </class> 
 
</hibernate-mapping>

Course.java

package com.my.bean; 
 
import java.util.HashSet; 
import java.util.Set; 
 
public class Course { 
 
    private Integer id; 
    private String content; 
 
    //不同课程可以被不同的学生学习(多对多) 
    //当前课程可以被【多个学生】学习 
    private Set<Student> studentSet = new HashSet<Student>(); 
 
    public Integer getId() { 
        return id; 
    } 
 
    public void setId(Integer id) { 
        this.id = id; 
    } 
 
    public String getContent() { 
        return content; 
    } 
 
    public void setContent(String content) { 
        this.content = content; 
    } 
 
    public Set<Student> getStudentSet() { 
        return studentSet; 
    } 
 
    public void setStudentSet(Set<Student> studentSet) { 
        this.studentSet = studentSet; 
    } 
 
} 

Course.hbm.xml

<?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.my.bean.Course" table="t_course"> 
        <id name="id"> 
            <generator class="native"></generator> 
        </id> 
        <property name="content"></property> 
 
        <!-- 不同课程可以被不同的学生学习(多对多), 当前课程可以被【多个学生】学习 
            * 1 集合 # 中间表表名 
            * 2 中间表中外键名称 
            * 3 类型:另一个外键名称 
         --> 
        <set name="studentSet" table="t_student_course" > <!-- cascade="delete" --> 
            <key column="course_id"></key> 
            <many-to-many class="com.my.bean.Student" column="student_id"></many-to-many> 
        </set> 
 
    </class> 
 
</hibernate-mapping> 

测试类:

package com.my.bean; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 
import org.junit.Test; 
public class TestManyToMany { 
private SessionFactory factory = new Configuration() 
.configure() 
.addClass(Student.class) 
.addClass(Course.class) 
.buildSessionFactory(); 
@Test 
public void demo01(){ 
Session session = factory.openSession(); 
session.close(); 
} 
@Test 
public void demo02(){ 
// 保存 学生 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Student student = new Student(); 
student.setName("jack"); 
session.save(student); 
transaction.commit(); 
session.close(); 
} 
@Test 
public void demo03(){ 
// 保存 课程 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Course course = new Course(); 
course.setContent("java web"); 
session.save(course); 
transaction.commit(); 
session.close(); 
} 
@Test 
public void demo04(){ 
// 保存学生  ,学生关联课程  -- 抛异常 (需要保存课程) 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Student student = new Student(); 
student.setName("rose"); 
Course course = new Course(); 
course.setContent("java 基础"); 
//关系 
student.getCourseSet().add(course);          
session.save(course);    
session.save(student);   
transaction.commit(); 
session.close(); 
} 
@Test 
public void demo05(){ 
/* 保存学生  ,学生关联课程 
* 自动保存:学生在保存时,课程一并保存,需要在Student.hbm.xml配置 cascade="save-update" 
*/ 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Student student = new Student(); 
student.setName("tom"); 
Course course = new Course(); 
course.setContent("java ssh"); 
//关系 
student.getCourseSet().add(course);  
session.save(course);    
session.save(student);  
transaction.commit(); 
session.close(); 
} 
@Test 
public void demo06(){ 
/* 默认情况:当删除学生时,先将中间表的数据删除 
* 级联删除:在删除学生时,先删除中间表的数据,级联删除课程内容 
* Student.htm.xml 配置 , cascade="save-update,delete"> 
*/ 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Student student = (Student) session.get(Student.class, 5); 
session.delete(student); 
transaction.commit(); 
session.close(); 
} 
@Test 
public void demo07(){ 
//准备数据 demo08 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Student student = new Student(); 
student.setName("jack"); 
Student student2 = new Student(); 
student2.setName("tom"); 
Student student3 = new Student(); 
student3.setName("rose"); 
Course course = new Course(); 
course.setContent("java 基础"); 
Course course2 = new Course(); 
course2.setContent("java web"); 
Course course3 = new Course(); 
course3.setContent("java ssh"); 
//关系 
student.getCourseSet().add(course); 
student.getCourseSet().add(course2); 
student.getCourseSet().add(course3); 
student2.getCourseSet().add(course2); 
student2.getCourseSet().add(course3); 
student3.getCourseSet().add(course3); 
session.save(student);  
session.save(student2);  
session.save(student3);  
transaction.commit(); 
session.close(); 
} 
@Test 
public void demo08(){ 
/* 双向级联删除 
*  Student.hbm.xml cascade="delete" 
*  Course.hbm.xml cascade="delete" 
*/ 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Student student = (Student) session.get(Student.class, 4); 
session.delete(student); 
transaction.commit(); 
session.close(); 
} 
@Test 
public void demo09(){ 
/* 前提:没有其他的配置 
*  查询学生,查询课程,将当前课程从学生的选课中移除 
*  * 将中间表的数据进行删除 
*/ 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Student student = (Student) session.get(Student.class, 7); 
Course course = (Course) session.get(Course. class, 4); 
student.getCourseSet().remove(course); 
transaction.commit(); 
session.close(); 
} 
} 

注意
1 默认:当删除学生时,先将中间表的数据删除。(中间表的数据删除时,不会影响另一个多表的数据)
2 默认:取消关联关系时,将中间表的数据删除
student.getCourseSet().remove(course);
关联关系,cascade=”all|none|save-update|delete|all-delete-orphan|delete-orphan”
none : 没有关系
save-update : 如果需要保存则进行保存,如果需要更新则进行更新,执行saveOrUpdate()
delete : 级联删除
delete-orphan : 孤儿删除 (1对多)
all 除了孤儿删除其他都支持
all-delete-orphan 所有级联

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/12086.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论