Detailed description of inverse usage in Hibernate (or other ORM). The content is taken from the Java web lightweight development interview tutorial, orminverse

Source: Internet
Author: User

Detailed description of inverse usage in Hibernate (or other ORM). The content is taken from the Java web lightweight development interview tutorial, orminverse

This article is an excerpt from the java web lightweight development interview tutorial.

The English meaning of Inverse is reversal. In Hibernate, it is used to determine which party maintains the association between two business entity classes. Specifically, which side sets the field value restricted by the foreign key.

The default value is false. That is to say, the local end (for example, if inverse = false is written on the student end, the local end is a student, and the other side is a course) does not "reverse control ", in this case, the local end maintains the association. If neither side is written, both ends are maintained. This will cause problems, that is, when both ends of the update are controlled, the update may be repeated.

Note that inverse only specifies who sets the foreign key value, rather than cascade operations. cascade is responsible for cascade operations. Many people will confuse their meaning and usage.

The data table used in this example is the Person table and the Card table. The Person table contains the ID, Name, and Phone fields, and the Card table contains the CardID, PersonID, Bank, and balance fields.

In the hibernate. cfg. xml file, use mapping resource to specify the corresponding ing file. The key code is as follows:

1 <! -- Add the ing file of the object class --> 2 <mapping resource = "Model/Card. hbm. xml "/> 3 <mapping resource =" Model/Person. hbm. xml "/>

In Person. hbm. xml, use the following code to specify the relationship between a Person (one party) and a card (multiple parties). inverse is true at the Person end.

1<set name="cards" cascade="save-update" inverse="true">2<key column="PersonID"/>3<one-to-many class="Card"/>4</set>

In HibernateMain. java, the following key code is used to insert a person's information.

1 // create a Card 2 Card card1 = new Card (); 3 card1.setCardID ("Card123"); 4 card1.setPersonID ("Person123"); 5 card1.setBank ("Citi "); 6 card1.setBalance (100f); 7 // initialize a Person 8 person Person = new person (); 9 Person. setID ("Person123"); 10 person. setName ("Peter"); 11 person. setPhone ("123456"); 12 // Add card113 set <Card> cards = new HashSet <Card> (); 14 cards to the cards Set. add (card1); 15 person. setCards (cards); 16 // Save the information of the person 17 session. save (person); 18 session. flush ();

After running, the following two insert statements are displayed in the output: one is insert Person information, and the other is Insert card information.

1 Hibernate: insert into Person (Name, Phone, ID) values (?, ?, ?)

2 Hibernate: insert into Card (PersonID, Bank, Balance, Person, CardID) values (?, ?, ?, ?, ?)

When the inverse in person. hbm. xml is set to false, an update statement is displayed.

1 Hibernate: insert into Person (Name, Phone, ID) values (?, ?, ?)

2 Hibernate: insert into Card (PersonID, Bank, Balance, Person, CardID) values (?, ?, ?, ?, ?)

3 Hibernate: update Card set PersonID =? Where CardID =?

The reason is that when inverse is set to true, the Person end reverses the control of the foreign key, that is, the Card end manages the foreign key. In the code, we only insert the Person, no Card is inserted, so there is no operation to update two foreign keys (PersonID and CardID. On the contrary, when inverse is set to false, the control of the management foreign key is on the Person side. When the Person is inserted, Hibernate needs an additional update statement to manage the foreign key.

In a one-to-many example, inverse does not affect the result regardless of the value, so it is easy to ignore its function.

In one-to-multiple cases, the control of foreign keys is usually managed by multiple parties. For example, if a Person opens 100, if the Person side manages the control, 100 more update operations may be performed, which is inefficient.

If inverse only affects efficiency in one-to-many cases, the inverse setting may affect data in many-to-many cases.

Let's take a look at many-to-many student course selection cases. One student can select multiple courses, while one course can contain multiple students. In Student. hbm. xml, an inverse = "true" statement can be added to statements that describe many-to-many relationships.

1<set name="courses" table="students_courses" inverse= "true" cascade="save-update">2<key column="student_id"></key>3<many-to-many class="Model.Course" column="course_id"></many-to-many>4</set>

In Course. hbm. xml, no statements about inverse are added. That is, the foreign key control is reversed on the Student end and the control is handed over to the Course end.

In the HibernateMain class, the following code allows s1 students to take computer courses.

1 // set a Student with the Student ID 12 Student s1 = new Student (); 3 s1.setStudentID ("1"); 4 s1.setStudentName ("Peter "); 5 // set multiple courses 6 Course c1 = new Course (); 7 c1.setCourseID ("c1"); 8 c1.setCourseName ("Math "); 9 Course c2 = new Course (); 10 c2.setCourseID ("c2"); 11 c2.setCourseName ("Java"); 12 Course c3 = new Course (); 13 c3.setCourseID ("c3"); 14 c3.setCourseName ("C #"); 15 // Set the Set16 Set <Course> computerCourses = new HashSet <Course> (); 17 computerCourses. add (c2); 18 computerCourses. add (c3); 19 // Let s1 students take computer courses (that is, c2 and c3 courses) 20 s1.setCourses (computerCourses); 21 // Save the s122 session. save (s1); 23 session. flush ();

The execution result is that although relevant Student and Course records can be viewed in the Student and Course tables, no association records can be found in the students_courses table. The reason is that the foreign key management right has been handed over to the Course side by setting inverse. Here, only the student is saved and the Course is not saved, so there is no action to insert the foreign key. To insert a foreign key Association in the students_courses table, set inverse to false in person. hbm. xml.

Therefore, if the inverse value is set incorrectly in many-to-many associations, the result may be incorrect. Please set the value based on the specific project.

 

 

  

 

  

 

 

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.