Hibernate Learning (5) Hibernate Many-to-Many Mapping

When it comes to the many-to-many relationship, the most impressive thing is the optional courses in universities.A student can take more than one course, a course can have more than one student, and each course he or she chooses has results.The E-R diagram for this scene is as follows:

For many-to-many relationships, we usually pull out an intermediate table (join table) to maintain the many-to-many relationship between the two tables. For example, the table structure that these relationships should generate is:

PO Object

   Student.java

package entity;

import java.util.Set;

public class Students {
    private int id ;
    private String name;
    private Set<Course>courses;
    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<Course> getCourses() {
        return courses;
    }
    public void setCourses(Set<Course> courses) {
        this.courses = courses;
    }
    
}

Course.java

package entity;

import java.util.Set;

public class Course {
   private int id;
   private String name;
   private Set<Students> students;
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<Students> getStudents() {
    return students;
}
public void setStudents(Set<Students> students) {
    this.students = students;
}
   
}

Mapping File

Students.hnm.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 ="entity.Students" table="t_students">
       <id name="id">
          <generator class="native"/>
       </id>
       <property name="name"/>
       <set name="Courses" table="t_singup">
        <key column="student_id"/>
         <!-- Multiple Students id Corresponding pairs course_id -->
<many-to-many class="entity.Course" column="course_id"></many-to-many> </set> </class> </hibernate-mapping>

The set in the configuration file corresponds to the set in the corresponding class, the key is a foreign key to the more-directed side, corresponding to t_Course_in score tableID.

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="entity.Course" table="t_course">
    <id name="id">
      <generator class="native"/>
    </id>
    <property name="name"/>
    <set name="students" table="t_signup" inverse="true">
     <key column="course_id"/>
     <many-to-many class="entity.Students" column="student_id"/>
    </set>
    </class>
</hibernate-mapping>    

The table-building statements executed by running the code are:

drop table  t_course
drop table  t_signup
drop table  t_students

create table t_course (id integer primary key , name varchar(255));
create sequence course
minvalue 1  --minimum value
nomaxvalue  --Do not set maximum
start with 1 --Count from 1
increment by 1 --Add 1 at a time
nocycle  --Always accumulating,No cycle
nocache --No Buffer
create  or replace trigger course_tg
before insert on t_course for each row when(new.id is null)
begin
select course.nextval into:new.id from dual;
end;
create table t_signup (student_id integer , course_id integer ,constraint zhujian primary key (student_id, course_id));
drop sequence students;
create table t_students (id integer , name varchar(255), primary key (id));
create sequence students
minvalue 1  --minimum value
nomaxvalue  --Do not set maximum
start with 1 --Count from 1
increment by 1 --Add 1 at a time
nocycle  --Always accumulating,No cycle
nocache ;--No Buffer
create  or replace trigger students_tg
before insert on t_students for each row when(new.id is null)
begin
select students.nextval into:new.id from dual;
end;
create index FK7DADC3438FFF3382 on t_signup(student_id);
alter table t_signup add constraint FK7DADC3438FFF3382 foreign key(student_id) references t_students(id);
create index FK7DADC3438CBEF332  on t_signup(course_id);
alter table t_signup add constraint  FK7DADC3438CBEF332 foreign key(course_id) references t_course(id);

The resulting table structure is as follows:

T_A composite primary key is generated in signup, student_id and course_id refers to t_students and t_Foreign key for course.

Insert Test

session.beginTransaction();

Course course1=new Course();
course1.setName("<Psychological Stress Microreactions");
session.save(course1);
Course course2=new Course();
course2.setName("<Harry·Porter and Genetics");
session.save(course2);
Course course3=new Course();
course3.setName("<Three Kingdoms Killing Strategy Tutorial");
session.save(course3);
Course course4=new Course();
course4.setName("<Video Appreciation of Parasitic and Parasitic Diseases");
session.save(course4);

Student student1=new Student();
Set courses1=new HashSet();
courses1.add(course1);
courses1.add(course2);
student1.setCourses(courses1);
student1.setName("Xiao Hu");
session.save(student1);

Student student2=new Student();
Set courses2=new HashSet();
courses2.add(course3);
courses2.add(course4);
student2.setCourses(courses2);
student2.setName("Xiaoyu");
session.save(student2);

Student student3=new Student();
Set courses3=new HashSet();
courses3.add(course1);
courses3.add(course2);
courses3.add(course3);
courses3.add(course4);
student3.setCourses(courses3);
student3.setName("ten-cent or twenty-cent silver coin");
session.save(student3);

session.getTransaction().commit();

Insert results:

 

However, the above method is not suitable for adding additional attributes to many-to-many relationships. What should I do?You can use two one-to-many relationships, that is, you can manually design the intermediate table as an entity and configure mapping relationships for it, so in general, a many-to-many relationship can also be implemented with two one-to-many relationships.

Tags: Java Hibernate Session xml

Posted on Wed, 20 May 2020 12:38:45 -0400 by phpbeginner0120