Hibernate Learning Notes (v)-many-to-many relationship mappings

Source: Internet
Author: User

Many-to-many relationship mappings

Many-to-many relationships are equivalent to inserting one row of data into the third table

A many-to-many cancellation relationship is equivalent to deleting one row of data in the third table

A many-to-many modification relationship is equivalent to the first deletion of the third table after adding

Many to many who maintain efficiency are the same. See demand

In the actual development process, many-to-many mapping relationships are more common.

Students choose a class example, a student can choose more than one course, a course can also be selected by multiple students, so that the formation of many-to-many mapping relationship

public class Student implements Serializable {private static final long Serialversionuid = 1l;private long sid;private Str ing name;private set<course> courses = new hashset<course> ();}

public class Course implements Serializable {private static final long Serialversionuid = 1l;private long cid;private stri ng name;private set<student> students = new hashset<student> ();}
*hbm.xml

<class name= "Student" table= "Student" ><id name= "Sid" ><generator class= "native" ></generator ></id><property name= "name" ></property><set name= "courses" table= "Student_courses  " Inverse= "false" cascade= "Save-update" ><key column= "Sid" ></key><many-to-many class= "Course" column= "CID" ></many-to-many></set><!--two ID writing, remember--></class>
<class name= "Course" table= "Course" ><id name= "CID" ><generator class= "native" ></generator> </id><property name= "name" ></property><!--table Third table name: Do not write will be generated by default, there may be two third table--><set Name= "Students" table= "student_courses" ><!--foreign key--><key column= "CID" ></key><!--column: foreign Key-- ><many-to-many class= "Student" column= "Sid" ></many-to-many></set></class>

Test class:

public class Many2manytest {private Session session;private Transaction Transaction; @Beforepublic void init () {Session = H Ibernateutils.opensession (); transaction = Session.begintransaction ();} /** * Student Cascade Operation * Save the student's time, Cascade Save course */@Testpublic void Testsavestudent_cascade_savecourse () {Student Student = new Student (); St Udent.setname ("A"); Course Course = new Course (); Course.setname ("a"); set<course> courses = new hashset<course> (); set<student> students = new hashset<student> (); Courses.add (course); student.setcourses (courses); Students.add (student); Session.save (student);//session.save (course);} /** * There is already a student, a course, associated with the relationship between */@Testpublic void testbuildrelationship () {Student Student = (Student) session.get ( Student.class, 2L); Course Course = (Course) session.get (Course.class, 2L);//system.out.println (Student.getcourses (). Size ()); 0, stating that even if the student does not have a curriculum,set<course> hibernate to help us create, if the student class is persisted//in the recommendation class defined set<>, the direct new out hashset/** * Student.getcourses (). Add (course); course.getstuDents (). Add (student); Error, there can only be one to add the operation,//from the students to establish a relationship or from the curriculum to establish a relationship can be */student.getcourses (). Add (course); /** * There is already a course, creating a new student, building a relationship */@Testpublic void Testsavestudent_buildr () {Course Course = (Course) session.get (course.class , 2L); Student Student = new Student () student.setname ("BB");//student.getcourses (). Add (course);//If you do not define set<> = new HASHSET&LT;&GT;, null pointer exception, 3 sql/*set<course> courses = new hashset<course> (); Courses.add (Course); Student.setcourses (courses); 3 sql*///course.getstudents (). Add (student);//4 Sqlstudent.getcourses (). Add (Course) /** * Hibernate program, efficiency and writing have a great relationship */session.save (student);} /** * A student from a course, transfer another course * SID for 5 students from Course 2 to course 1 */@Testpublic void Testtransform () {Student Student = (Student) session.get (St UDENT.CLASS,5L); Course Course = (Course) session.get (Course.class, 1L); Course Course2 = (Course) session.get (Course.class, 2L); student.getcourses (). Remove (COURSE2);// Relieve students and CID for 2 of course student.getcourses (). Add (course);//6 SQL,JDBC Just update, that's where Hibernate's pit Daddy}/** * Lift a student andThe relationship between all the courses that the student learns * * * @Testpublic void Testrealseallr () {/** * to dismiss the relationship between the SID 1 student and all courses */student Student = (Student) Session.get (Student.class, 1L); student.setcourses (null); Transaction.commit (); Session.close ();} /** * Relieves the relationship between a student and all classes, and then establishes the relationship between some courses */@Testpublic void Testrealseallrandbuildr () {Student Student = (Student) Session.get (Student.class, 1L);/** * Because of the relationship between some courses, so use set, do not have an add */set<course> courses = new hashset< Course> (); Course Course = (Course) session.get (Course.class, 3L); Courses.add (Course);// Student the original courses to cover the student.setcourses (courses);} @Afterpublic void Destory () {transaction.commit (); Session.close ();}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate Learning Notes (v)-many-to-many relationship mappings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.