Hibernate - many-to-many relationship mapping

Many-to-many relationship mapping

The many-to-many relationship is not ideal in terms of operation and performance, so the many-to-many mapping is less used, and it is best to convert it into a one-to-many object model in actual use; Hibernate will create an intermediate table for us and convert it into two One-to-many.

Classic case: student <–> course selection, as follows to associate the student course selection relationship with an intermediate table.

Conversion of many-to-many relationships

This case was also used when we were learning HQL statements, so I will briefly introduce it here. For more details, please refer to [ Hibernate - HQL Statement ]

The domain objects and relationship mapping files of the three objects are as follows:

  • Student object and relationship mapping file:
package com.gavin.domain;

import java.util.Set;

public class Student {
    private int id;
    private String name;
    private Set<StudentCourse> studentCourseSet;

    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 Set<StudentCourse> getStudentCourseSet() {
        return studentCourseSet;
    }

    public void setStudentCourseSet(Set<StudentCourse> studentCourseSet) {
        this.studentCourseSet = studentCourseSet;
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.gavin.domain.Student" table="student" schema="hibernate">
        <id name="id" type="java.lang.Integer" column="id">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>
        <set name="studentCourseSet">
            <key column="sid"></key>
            <one-to-many class="com.gavin.domain.StudentCourse"/>
        </set>
    </class>
</hibernate-mapping>
  • Course object and relationship mapping file:
package com.gavin.domain;

import java.util.Set;

public class Course {
    private int id;
    private String name;
    private Set<StudentCourse> studentCourseSet;

    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 Set<StudentCourse> getStudentCourseSet() {
        return studentCourseSet;
    }

    public void setStudentCourseSet(Set<StudentCourse> studentCourseSet) {
        this.studentCourseSet = studentCourseSet;
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.gavin.domain">
    <class name="com.gavin.domain.Course" table="course" schema="hibernate">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>
        <set name="studentCourseSet">
            <key column="cid"></key>
            <one-to-many class="com.gavin.domain.StudentCourse"></one-to-many>
        </set>
    </class>
</hibernate-mapping>
  • StudentCourse object and relationship mapping file:
package com.gavin.domain;

public class StudentCourse {
    private int id;
    private Student student;
    private Course course;
    private int grade;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.gavin.domain.StudentCourse" table="studcourse" schema="hibernate">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="increment"/>
        </id>
        <property name="grade" type="java.lang.Integer" column="grade"/>
        <many-to-one name="student" column="sid"/>
        <many-to-one name="course" column="cid"/>
    </class>
</hibernate-mapping>
  • Test code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

Student student = new Student();
student.setName("虾米");

Course course = new Course();
course.setName("数学");

StudentCourse studentCourse = new StudentCourse();
studentCourse.setStudent(student);
studentCourse.setCourse(course);
studentCourse.setGrade(89);

session.save(student);
session.save(course);
session.save(studentCourse);

transaction.commit();
  • The SQL statement generated by Hibernate is:
Hibernate: drop table if exists course
Hibernate: drop table if exists studcourse
Hibernate: drop table if exists student
Hibernate: create table course (id integer not null, name varchar(255), primary key (id)) engine=MyISAM
Hibernate: create table studcourse (id integer not null, grade integer, sid integer, cid integer, primary key (id)) engine=MyISAM
Hibernate: create table student (id integer not null, name varchar(255), primary key (id)) engine=MyISAM
Hibernate: alter table studcourse add constraint FK3e0p086wyy1y3198jmcl1p07e foreign key (sid) references student (id)
Hibernate: alter table studcourse add constraint FKdxogu0d4je13yayrhem591hvn foreign key (cid) references course (id)
Hibernate: select max(id) from student
Hibernate: select max(id) from course
Hibernate: select max(id) from studcourse
Hibernate: insert into student (name, id) values (?, ?)
Hibernate: insert into course (name, id) values (?, ?)
Hibernate: insert into studcourse (grade, sid, cid, id) values (?, ?, ?, ?)
  • The resulting table structure is as follows:

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325655142&siteId=291194637