Many-to-many relationships (student student, course course)
The definition of student class and the configuration of HBM files are as follows
1 Public class Student {2 Private int ID; 3 Private String name; 4 Private New Hashset<course>(); 5 }
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE hibernate-mapping Public3 "-//hibernate/hibernate Mapping DTD 3.0//en"4 "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">5 <hibernate-mapping>6 <classname= "Com.zlt.hibernatedemo.Student"Table= "Student">7 <IDname= "id"column= "id">8 <Generatorclass= "Increment"></Generator>9 </ID>Ten One < Propertyname= "Name"type= "Java.lang.String"> A <columnname= "Name"length= " the" /> - </ Property> - the <Setname= "Courses"Table= "Score"Cascade= "All"> - <Keycolumn= "StudentID"></Key> - <Many-to-manyclass= "Com.zlt.hibernatedemo.Course"column= "CourseID"/> - </Set> + </class> - + </hibernate-mapping>
The definition of course class and the configuration of HBM files are as follows
1 Public class Course {2 Private int ID; 3 Private String Coursename; 4 Private New Hashset<student>(); 5 }
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE hibernate-mapping Public3 "-//hibernate/hibernate Mapping DTD 3.0//en"4 "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">5 <hibernate-mapping>6 <classname= "Com.zlt.hibernatedemo.Course"Table= "Course">7 <IDname= "id"column= "id">8 <Generatorclass= "Increment"></Generator>9 </ID>Ten One < Propertyname= "Coursename"></ Property> A - <Setname= "Students"Table= "Score"> - <Keycolumn= "CourseID"></Key> the <Many-to-manyclass= "Com.zlt.hibernatedemo.Student"column= "StudentID"/> - </Set> - - </class> + - </hibernate-mapping>
Test program
1 Public classHibernatetest {2 Public Static voidMain (string[] args) {3Session session =hibernatefactory.currentsession ();4Transaction tx =session.begintransaction ();5 6 7Student Student1 =NewStudent ();8Student1.setname ("Student1");9 TenStudent Student2 =NewStudent (); OneStudent2.setname ("Student2"); A -Course course1 =NewCourse (); -Course1.setcoursename ("Course1"); the -Course Course2 =NewCourse (); -Course2.setcoursename ("Course2"); - + //because the default inverse (false) is used, both sides can maintain association relationships - student1.getcourses (). Add (course1); + student1.getcourses (). Add (COURSE2); A at - Session.save (student1); - //student Cascade is set, so no additional insertions are required course - //Session.save (COURSE1); - //Session.save (COURSE2); - in tx.commit (); - session.close (); to + } -}
Results
If there are other attributes in many-to-many auxiliary tables, you have to split the many-to-many relationship into two one-to-many relationships
Student Student
1 Public class Student {2 Private int ID; 3 Private String name; 4 Private New Hashset<score>(); 5 }
Course Course
1 Public class Course {2 Private int ID; 3 Private String Coursename; 4 Private New Hashset<score>(); 5 }
Score Score
1 Public classScore {2 Private intID;3 PrivateStudent Student;4 PrivateCourse Course;5 6 //Additional Properties7 Private intscore;8}