Many to many:
* The link is in the third table and has no relationship with the two tables themselves.
* Many-to-many relationship maintenance: Anyone can maintain the relationship (the efficiency is the same). Generally, the relationship can be maintained through the page.
To reflect
* Link embodiment:
Maintenance of the third table: add and delete
Couring file corresponding to the course class
Stuing file corresponding to the student table
Multi-to-Multi-scenario example
Import java. lang. annotation. target; import java. util. hashSet; import java. util. set; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. junit. test;/*** 1. Save course ** 2. Save student * 3. Save course * 4. Save course * 4. Save course, and establish the relationship between the course and the student * 5. There is already a course, create a student, and establish the relationship between the student and the course * 6. There is already a student, create a new course and establish the relationship between the student and the course * 7. A student already exists and has After a class exists, the relationship between the student and the original class is removed, and the relationship between the student and the new class is established * 8. A student already exists, disassociate the student from the student's class * 9. disassociate the student from the class and all the students, re-establish the relationship between the class and some new students * 10. Undo the relationship between the class and all students * 11. Delete the class * release the class and all students. between students ** delete this class ** Delete the students while deleting the class ** 12 Delete the students * Delete the same class * @ author Administrator **/public class ManyToManyTest {private static SessionFactory sessionFactory = null; static {Configuration configuration = new Configuration (); configuration. conf Igure ("cn/itcast/hibernate0909/manytoure/hibernate. cfg. xml "); sessionFactory = configuration. buildSessionFactory ();}/*** save course */@ Testpublic void testSaveCourse () {Session session = sessionFactory. openSession (); Transaction transaction = session. beginTransaction (); Course course = new Course (); course. setCname ("physiological health"); course. setDescription ("all speak hygiene"); session. save (course); transaction. commit (); session. Close ();}/*** save student */@ Testpublic void testSaveStudent () {Session session = sessionFactory. openSession (); Transaction transaction = session. beginTransaction (); Student student = new Student (); student. setSname ("alive"); student. setDescription ("UFO"); session. save (student); transaction. commit (); session. close ();}/*** save course while saving student * Hibernate: select max (cid) from CourseHibernate: select max (sid) from StudentH Ibame: insert into Course (cname, description, cid) values (?, ?, ?) Hibernate: insert into Student (sname, description, sid) values (?, ?, ?) Insert a row of data into the Course table and Student table respectively. The ing file shows that Student maintains the relationship, but the code for maintaining the link cannot be found from the Code */@ Testpublic void testSaveCourse_Cascade () {Session session = sessionFactory. openSession (); Transaction transaction = session. beginTransaction ();/** create a Course */course Course = new course (); Course. setCname ("java basics"); course. setDescription ("Lecture");/*** create a Student */student Student = new student (); Student. setSname ("smug: Ximen Qing"); student. setDescription ("master"); Set
Students = new HashSet
(); Students. add (student);/*** establish the relationship between the course and the student through the course */course. setStudents (students); // because the course is new, no student session is required. save (course); transaction. commit (); session. close ();}/*** Save the course and save the student, and establish the relationship */@ Testpublic void testSaveCourse_Cascade_R () {Session session = sessionFactory. openSession (); Transaction transaction = session. beginTransaction ();/*** create Course */course Course = new course (); Course. setCname ("java advanced"); course. setDescription ("lecture framework, represented by Jin Yunlong"); Set
Courses = new HashSet
(); Courses. add (course);/*** create Student */student Student = new student (); Student. setSname ("Shift Leader"); student. setDescription ("Master, Yuan Fang, what do you think? ");/** Establish the relationship between students and courses through students */student. setCourse (courses); session. save (student); transaction. commit (); session. close ();}/*** a course already exists. Create a new student and establish the relationship between the student and the course * Hibernate: select course0 _. cid as cid0_0 _, course0 _. cname as cname0_0 _, course0 _. description as descript3_0_0 _ from Course course0 _ where course0 _. cid =? Hibernate: select max (sid) from studenthib.pdf: insert into Student (sname, description, sid) values (?, ?, ?) Hibernate: insert into student_course (sid, cid) values (?, ?) This SQL statement maintains the relationship */@ Testpublic void testSaveStudent_R () {Session session = sessionFactory. openSession (); Transaction transaction = session. beginTransaction (); /*** 1. Find a Course. 2. Create a student. 3. Maintain the relationship between the student and the course. * // search for Course 2 course Course = (course) session. get (Course. class, 2L); // create a Student student Student = new student (); Student. setSname ("Yi mensheng"); student. setDescription ("aa"); // sets the relationship between the student and the course through the student
Courses = new HashSet
(); Courses. add (course); student. setCourse (courses); session. save (student); transaction. commit (); session. close ();}/*** a student already exists. Create a new course and establish the relationship between the student and the course */@ Testpublic void testSaveCourse_R () {Session session = sessionFactory. openSession (); Transaction transaction = session. beginTransaction (); /*** 1. Create a Course ** 2. Search for students ** 3. Add a new course based on the student's original Course * // create a Course course Course = new Course (); course. setCname ("project course"); course. setDescription ("satisfied"); // find Student student = (Student) session. get (Student. class, 5L); Set
Courses = student. getCourse (); // obtain all current courses of this student. add (course); transaction. commit (); session. close ();}}