"Hibernate" in the inverse= "true" in Hibernate

Source: Internet
Author: User

First two classes, one is class class, one is student class:

public class grade{      private int id;      private String name;     Private Set students = new HashSet ();}

public class Student {      private int id;      Private String Studentname;}

Structure of tables in the database:

T_grade: Two fields: ID name

T_student: Three fields: ID studentname gradeid


Mapping file for the Grade class: Grade.hbm.xml(This is a one-way association)

Now execute the following Java code:

Set students = new HashSet (); Student S1 = new Student (); S1.setstudentname ("S1"); Student O2 = new Student (); S2.setstudentname ("S2"); Students.add (S1); Students.add (S2);  Grade g = new Grade (); G.setname ("G1"); g1.setstudents (students)  ; Session.save (c);

The SQL statement that hibernate sends out at this point is as follows:

Hibernate:insert into T_grade (name) VALUES (?) Hibernate:insert into T_student (studentname) VALUES (?) Hibernate:insert into T_student (studentname) VALUES (?) Hibernate:update t_student set gradeid=? where id=? Hibernate:update t_student Set Gradeid =? where id=?

The query database is now displayed as follows:

T_grade:

ID | Name

1 G1

T_student:

ID |   Studentname | Gradeid

1 S1 1

2 S2 2


When you save a grade object, insert into T_grade (?) is emitted first. The G1 statement synchronizes the data to the database because cascade= "Save-update" is set in the <set> map, so s1, S2 two objects are saved simultaneously. If cascade= "Save-update" is not set in the mapping file, hibernate does not automatically save the students collection object, which throws an exception on update:

Hibernate:insert into T_grade (name) VALUES (?) Hibernate:update t_student set gradeid=? where id=?org.hibernate.transientobjectexception:student

Exception Analysis:

Because the inverse attribute in <set> is "Inverse=false" by default, it is the grade object that is responsible for the maintenance of the associated relationship, which is responsible for updating Gradeid in the T_student table. However, because the casade= "Save-update" is not set, the objects in the students collection are not automatically saved when the grade is saved, so an exception is thrown.


Reset Casade= "Save-update" and set inverse= "true", as follows:

Also execute the above Java code, Hibernate sends the following statements:

Hibernate:insert into T_grade (name) VALUES (?) Hibernate:insert into T_student (studentname) VALUES (?) Hibernate:insert into T_student (studentname) VALUES (?)

Now look at the database, which appears as follows:

T_grade:

ID | Name

1 G1

T_student:

ID |   Studentname | Gradeid

1 S1 NULL

2 S2 NULL

At this point the Gradeid of the t_student table is null, because inverse= "true" is set, so student is the master at this time, the maintenance of the correlation relationship is done by student , when the grade object is saved, Grade will not maintain the students Gradeid attribute, it must be maintained by the student itself, that is to say Student.setgrade (grade) must be set;

When a relationship is maintained through student, the association is transformed into a bidirectional association .

To add a line of code to the Student class, change to the following:

public class Student {private int id; private String studentname;private Grade Grade;}

At this point Student.hbm.xml also add a few lines of code:

If you execute the previous Java code again, hibernate emits the following statement:

Hibernate:insert into T_grade (name) VALUES (?) Hibernate:insert into T_student (Studentname,gradeid) VALUES (?,?) Hibernate:insert into T_student (Studentname,gradeid) VALUES (?,?)

Saving the Student object now assigns a value to Gradeid because the student object has the grade attribute, the Gradeid field, the database at this point, or the same as before:

T_grade:

ID | Name

1 G1

T_student:

ID |   Studentname | Gradeid

1 S1 NULL

2 S2 NULL

The value of Gradeid is also null because the Grade property of the student object is not set in the Java code above, and because we set the inverse= "true", the relationship is maintained by the student object. Therefore, you must add more than one line of Student.setgrade (grade) in the Java code;

Modify the code to:

Grade g = new Grade (); Set students = new HashSet (); Student S1 = new Student (); S1.setstudentname ("S1"); S1.setgrade (g); Student O2 = new Student () s2.setstudentname ("S2"); S2.setgrade (g); Students.add (S1); Students.add (S2);  G.setname ("G1"); g1.setstudents (students);  Session.save (c);

At this point, hibernate issues the following statement:

Hibernate:insert into T_grade (name) VALUES (?) Hibernate:insert into T_student (Studentname,gradeid) VALUES (?,?) Hibernate:insert into T_student (Studentname,gradeid) VALUES (?,?)

Then look at the database:

T_grade:

ID | Name

1 G1

T_student:

ID |   Studentname | Gradeid

1 S1 1

2 S2 1

The value of Gradeid has been set.

Summary: In a one-to-many association, setting inverse= "true" on a side to allow many parties to maintain association relationships is more helpful for optimization because it can reduce the execution of update statements.


Author: Gu

Sign: Don't lose to who you used to be














"Hibernate" in the inverse= "true" in Hibernate

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.