Hibernate a pair of one or one-to-many, many-to-many annotation attributes summary

Source: Internet
Author: User

Mappedby:

The content must be the field name of this class on the other side.

said: This class abandons the control association relations, all to the association relations control, such as: establishes, the dissolution and the other party's relations, all is controlled by the other side, this kind regardless. As an example:

Between teacher and student is a many-to-many association, in the @manytomany annotation of the teachers field on the student side, the configuration property Mappedby = "Students". said: Students are not qualified to control the relationship with the teacher (such as: Establish a relationship, delete the relationship), only the teacher is qualified to control the relationship. Students can't say, "Let's Be Friends." (Even if it does, it doesn't help, the teacher didn't say it when he was a student.) That is, hibernate treats the students as if they are not going to throw an exception, and the program can still run. But the teacher can say, "Let's Be Friends." Then the student can only accept without any reason to refuse. Likewise, if the student says, "Let's Clean Break", it's useless. Only the teacher said: "Roll, how far, how far, for the division today will you out of the door, never want to see you again", then, this can effectively disassociate relations (note: This is an object (corresponding to a row of records in the database) and an object relationship between the table and the table is not the association between. The dissolution of a teacher-student relationship cannot affect the relationship between teachers and students in the whole society.

Test code: (This is the complete class, only post the Test1 code, the other parts are unchanged)

 Public classMappedbytest {Private Staticsessionfactory sessionfactory; PrivateSession session; @BeforeClass Public Static voidbeforetest () {sessionfactory=hibernateutil.getsessionfactory (); } @AfterClass Public Static voidaftertest () {sessionfactory.close (); } @Before Public voidBeginTransaction () {Session=sessionfactory.opensession ();    Session.begintransaction (); } @After Public voidcommittransaction () {session.gettransaction (). commit ();    Session.close (); } @Test Public voidExecute () {test1 (); }         Public voidtest1 () {Student student1=NewStudent ("Student Zhang San", 3); Student Student2=NewStudent ("Student John Doe", 4); Teacher Teacher1=NewTeacher ("Teacher Harry", 5); Teacher Teacher2=NewTeacher ("Teacher Zhao Liu", 6); List<Student> students =NewArraylist<>();        Students.add (STUDENT1);        Students.add (Student2); List<Teacher> teachers =NewArraylist<>();        Teachers.add (Teacher1);                Teachers.add (TEACHER2);        Session.save (Teacher1);        Session.save (TEACHER2);        Student1.setteachers (teachers);    Session.save (STUDENT1); }}

The resulting result:

----------------------------------------------------------------------------------------

Then, clear the database and do a second test:

 Public voidtest1 () {Student student1=NewStudent ("Student Zhang San", 3); Student Student2=NewStudent ("Student John Doe", 4); Teacher Teacher1=NewTeacher ("Teacher Harry", 5); Teacher Teacher2=NewTeacher ("Teacher Zhao Liu", 6); List<Student> students =NewArraylist<>();        Students.add (STUDENT1);        Students.add (Student2); List<Teacher> teachers =NewArraylist<>();        Teachers.add (Teacher1);        Teachers.add (TEACHER2);        Session.save (STUDENT1);        Session.save (Student2);        Teacher1.setstudents (students);    Session.save (Teacher1); }

The resulting result:

Visible: In the student class of the teachers field of the @manytomany annotation configuration Properties Mappedby = "Students", all student class generated objects can not be established and the Teacher's association, only the teacher can.

----------------------------------------------------------------------------------------

Then, clear the database and do the third Test:

 Public voidtest1 () {Student student1=NewStudent ("Student Zhang San", 3); Student Student2=NewStudent ("Student John Doe", 4); Teacher Teacher1=NewTeacher ("Teacher Harry", 5); Teacher Teacher2=NewTeacher ("Teacher Zhao Liu", 6); List<Student> students =NewArraylist<>();        Students.add (STUDENT1);        Students.add (Student2); List<Teacher> teachers =NewArraylist<>();        Teachers.add (Teacher1);                        Teachers.add (TEACHER2); intStuid = 4; intTeacherid = 6; Student persistentstudent= (Student) session.get (Student.class, Stuid); Teacher Persistentteacher= (Teacher) session.get (Teacher.class, Teacherid);        Persistentstudent.getteachers (). Remove (Persistentteacher);    Session.update (persistentstudent); }

SQL statements typed by the console:

Hibernate:Selectstudent0_.student_id asstudent_1_0_0_, Student0_.age asage2_0_0_, Student0_.name asname3_0_0_ fromStudent student0_wherestudent0_.student_id=? Hibernate:Selectteacher0_.teacher_id asteacher_1_1_0_, Teacher0_.age asage2_1_0_, Teacher0_.name asname3_1_0_ fromTeacher Teacher0_whereteacher0_.teacher_id=? Hibernate:Selectteachers0_.student_id asstudent_2_2_0_, teachers0_.teacher_id asteacher_1_2_0_, teacher1_.teacher_id asteacher_1_1_1_, Teacher1_.age asage2_1_1_, Teacher1_.name asname3_1_1_ fromts_relation teachers0_Inner JoinTeacher Teacher1_ onteachers0_.teacher_id=teacher1_.teacher_idwhereteachers0_.student_id=?

The resulting result:

----------------------------------------------------------------------------------------

Then, clear the database and do the fourth Test:

 Public voidtest1 () {Student student1=NewStudent ("Student Zhang San", 3); Student Student2=NewStudent ("Student John Doe", 4); Teacher Teacher1=NewTeacher ("Teacher Harry", 5); Teacher Teacher2=NewTeacher ("Teacher Zhao Liu", 6); List<Student> students =NewArraylist<>();        Students.add (STUDENT1);        Students.add (Student2); List<Teacher> teachers =NewArraylist<>();        Teachers.add (Teacher1);                        Teachers.add (TEACHER2); intStuid = 4; intTeacherid = 6; Student persistentstudent= (Student) session.get (Student.class, Stuid); Teacher Persistentteacher= (Teacher) session.get (Teacher.class, Teacherid);        Persistentteacher.getstudents (). Remove (persistentstudent);    Session.update (Persistentteacher); }

SQL statements typed by the console:

Hibernate:Selectstudent0_.student_id asstudent_1_0_0_, Student0_.age asage2_0_0_, Student0_.name asname3_0_0_ fromStudent student0_wherestudent0_.student_id=? Hibernate:Selectteacher0_.teacher_id asteacher_1_1_0_, Teacher0_.age asage2_1_0_, Teacher0_.name asname3_1_0_ fromTeacher Teacher0_whereteacher0_.teacher_id=? Hibernate:Selectstudents0_.teacher_id asteacher_1_2_0_, students0_.student_id asstudent_2_2_0_, student1_.student_id asstudent_1_0_1_, Student1_.age asage2_0_1_, Student1_.name asname3_0_1_ fromts_relation students0_Inner JoinStudent student1_ onstudents0_.student_id=student1_.student_idwherestudents0_.teacher_id=? Hibernate: --------------------------Pay attention to this one  .Delete      fromts_relationwhereteacher_id=? Hibernate:Insert      intots_relation (teacher_id, student_id)Values        (?, ?)

The resulting result:

Visible: In the student class of the teachers field of the @manytomany annotation configuration Properties Mappedby = "Students", all student class generated objects can not be deleted and the teacher's association, only the teacher can.

----------------------------------------------------------------------------------------

As a result of this, Mappedby said that this type of prevention of control and the relationship between the other party, the content must be this class on the other side of the field name.

Hibernate a pair of one or one-to-many, many-to-many annotation properties summary

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.