This is still an example of the way to demonstrate that the relationship between students and teachers is a typical many-to-many relationship, a teacher can have more than one student, the same student can have more than one teacher. Storing in a database requires a student table to store student information, a teacher's table to store teacher information, and in order to represent the relationship between them we need an intermediate table to represent the connection between them.
For example, there are id,name two attributes in the teacher table, and id,name two attributes in the student table. There are two records in the Teacher's table (1, Mr. Dong), (2, Miss Li), and the students ' table also has two (1, Zhang San), (2, John Doe). There are two fields in the intermediate table (Teacherid,studentid), which form a federated primary key, and these two fields are the foreign keys of the teacher's table and the student table, and the teacher and the student are connected directly by inserting the ID of the associated teacher and student in the middle table, For example, inserting in the intermediate table (a) indicates that Mr. Dong is associated with Zhang San, and inserting (to) indicates that Mr. Dong is associated with John Doe.
Let's take a look at the teacher class and the student class
package entity;import Java.util.hashset;import Java.util.set;public class Teacher { Private long id;private String name;private set<student> students = new hashset<student> ();p ublic Long getId ( ) {return ID;} public void SetId (Long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public set<student> getstudents () {return students;} public void Setstudents (set<student> students) {this.students = students;}}
Package Entity;import Java.util.hashset;import Java.util.set;public class Student {private Long id;private String name; Private set<teacher> teachers = new Hashset<teacher> ();p ublic Long getId () {return ID;} public void SetId (Long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public set<teacher> Getteachers () {return teachers;} public void Setteachers (set<teacher> teachers) {this.teachers = teachers;}}
A collection is still used in the Pojo class to represent more than one record.
Here's a look at their mapping profile
First Look at Teacher.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 ">
The properties of the set type in the class are still mapped using the <set> tag in the mapping configuration file, and <many-to-many> in the <set> tag indicates that the relationship between them is many-to-many.
and watch 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 ">
In this way, many-to-many association mappings between teachers and students are configured, so look at the test classes below. This is done using Jutil to test
Package Test;import static Org.junit.assert.*;import Java.util.iterator;import Java.util.set;import Org.hibernate.session;import org.hibernate.transaction;import entity. Student;import entity. Teacher;import Factory. Hibernatesessionfactory;public class Test {private session session = Null;private Transaction tran = null;//Storage Object @org.juni t.testpublic void Save () {session = Hibernatesessionfactory.getsession (); tran = Session.begintransaction (); try { Teacher Teacher = new Teacher () Teacher.setid (1L); Teacher.setname ("Mr. Dong"); Teacher t2 = new Teacher () T2.setid (2l); T2.setname ("Miss Li"); Student S1 = new Student () S1.setid (1L); S1.setname ("John Doe"); Student s2 = new Student () S2.setid (2l); S2.setname ("Zhang San"); Teacher.getstudents (). Add (S1); Teacher.getstudents (). Add ( S2); t2.getstudents (). Add (S1); T2.getstudents (). Add (S2); S1.getteachers (). Add (teacher); S1.getteachers (). Add (T2); S2.getteachers (). Add (Teacher), S2.getteachers (). Add (T2); Session.save (teacher); Session.save (T2); Session.save (S1) ; Session.save (S2); Tran.commit ();} catch (Exception e) {tran.rollback ();}} Get teacher Info @org.junit.testpublic void Getteacher () {session = Hibernatesessionfactory.getsession (); tran = Session.begintransaction (); try {Teacher Teacher = (Teacher) session.get (Teacher.class, 1l); set<student> set = Teacher.getstudents (); System.out.println (Teacher.getname () + "student is:");iterator<student> it = Set.iterator (); while (It.hasnext ()) { Student s = it.next (); System.out.println (S.getname ());} Tran.commit ();} catch (Exception e) {tran.rollback ();}} Disassociate relationship @org.junit.testpublic void Removerelation () {session = Hibernatesessionfactory.getsession (); tran = Session.begintransaction (); try {Student s = (Student) session.get (Student.class, 1l); Teacher Teacher = (Teacher) session.get (Teacher.class, 1l);//If the Teacher property of inverse is false it can be lifted, If True, teacher.getstudents () can not be lifted. Remove (s); Tran.commit ();} catch (Exception e) {tran.rollback ();}} Delete Association relationship @org.junit.testpublic void Deleterelation () {session = Hibernatesessionfactory.getsession (); tran = SEssion.begintransaction (); try {Teacher Teacher = (Teacher) session.get (Teacher.class, 2l);// When the inverse property of teacher is false, the teacher information can be deleted and the related records in the intermediate table are deleted//when the inverse property is true when the teacher information is deleted when the exception Session.delete (teacher) is thrown; Tran.commit ();} catch (Exception e) {tran.rollback ();}}}
Many-to-many association relationships of hibernate relational mappings