Take the teacher-student relationship directly as an example. In a many-to-many association relationship, one of the parties can keep all the information of the other party through set, and the association is a bidirectional association. In a many-to-many association relationship, it can only be a two-way association. The teacher and student correspond to a table, which maintains many-to-many associations through an intermediate table with two IDs.
Entity class
Package Test.hibernate.hbmmanytomany;import Java.util.hashset;import Java.util.set;public class Teacher {private Integer id;private String name;private set<student> students = new hashset<student> ();p ublic 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<student> getstudents () {return students;} public void Setstudents (set<student> students) {this.students = students;} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "[teacher:id=" + ID + ", name=" + name + "]";}}
Package Test.hibernate.hbmmanytomany;import Java.util.hashset;import Java.util.set;public class Student {private Integer id;private String name;private set<teacher> teachers = new Hashset<teacher> ();p ublic 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<teacher> Getteachers () {return teachers;} public void Setteachers (set<teacher> teachers) {this.teachers = teachers;} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "[student:id=" + ID + ", name=" + name + "]";}}
Student.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
In a many-to-many relationship, only one of them can be maintained in set through the inverse setting.
Teacher.hbm.xml<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
Test classPackage Test.hibernate.hbmmanytomany;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.junit.test;public class App {private static sessionfactory Sessionfactory = New Configuration ()//.configure ()//.addclass (Teacher.class)//Add Hibernate entity class (load corresponding mapping file). addclass (Student.class)/ /.buildsessionfactory (); @Testpublic void Testsave () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//--------------------------------------------//Build object Teacher teacher1 = new Teacher (); Teacher1.setname ("Mr. Lin"); Teacher teacher2 = new Teacher (); Teacher2.setname ("Miss Zhang"); Student student1 = new Student (); Student1.setname ("Li Ming"); Student Student2 = new Student () student2.setname ("Liu Bei"); Teacher1.getstudents (). Add (Student1); Teacher1.getstudents ( ). Add (Student2); Teacher2.getstudents (). Add (Student1); Teacher2.getstudents (). Add (Student2);//The following section cannot be combined with the code block above, You can only select part of it, or you will throw an exception that duplicates the same record in the intermediate table//If both sides are to be written, note that the inverse of one side is set to true//sTudent1.getteachers (). Add (Teacher1);//Student1.getteachers (). Add (Teacher2);//Student2.getteachers (). Add ( TEACHER1);//Student2.getteachers (). Add (Teacher2);//Save Session.save (Teacher1); Session.save (Teacher2); Session.save (Student1); Session.save (student2);//-------------------------------------------- Session.gettransaction (). commit (); Session.close ();} Gets the employee associated with the Department @testpublic Void Testget () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//Get data teacher teacher = (teacher) session.get (Teacher.class, 4); System.out.println (teacher); System.out.println (Teacher.getstudents ()); Session.gettransaction (). commit (); Session.close ();} Disassociate relationship @testpublic void Testremoverelation () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//--------------------------------------------//if Teacher.hbm.xml in inverse= False can be deleted, otherwise it can not be deleted teacher teacher = (teacher) session.get (Teacher.class, 4); Teacher.getstudents (). Clear ();/--------------------------------------------session.gettransaction (). commit (); Session.close ();} Deletion of departments and their impact on employees @testpublic void Testdelete () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ()//--------------------------------------------//will also delete teachers in teacher_student and teacher id= 4 of records Teacher teacher = (teacher) session.get (Teacher.class, 4); Session.delete (teacher);//--------------------------- -----------------session.gettransaction (). commit (); Session.close ();}}
The table structure generated by the code
Console content when executing the Testget method
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If you want to reprint, please specify the source: Http://blog.csdn.net/lindonglian
Hibernate's implementation of many-to-many correlation mapping relationships