Inverse usage in Hibernate (or other ORM), excerpt from Java Web Lightweight Development Interview tutorial

Source: Internet
Author: User
Tags java web

This article comes from an excerpt from the Java Web Lightweight Development Interview tutorial.

The English meaning of inverse is inversion, which is used in hibernate to decide which party is to maintain the association between the two business entity classes, specifically, which side is going to set the field value of the FOREIGN KEY constraint.

Its default value is False, that is, this side (such as Inverse=false written on the student side, then this side is the student, the other side is the course) does not "reverse control", this awkward words of the other argument is that the local side to maintain the association relationship. If both sides are not written, then both ends are maintained. This creates a problem, that is, the new time because both sides control the relationship and therefore may cause duplicate updates.

Note that inverse simply specifies who will set the foreign key value, rather than the way it is used to set the cascade operation, which is handled by Cascade, and many people confuse their meaning and usage.

The data tables used in this example are the person and card tables, where the person table contains the ID, name, and phone fields, and the card table contains four fields of Cardid, PersonID, bank, and balance.

In the Hibernate.cfg.xml file, the corresponding mapping file is specified by mapping resource, where the key code is as follows:

1<!--Add a mapping file for an entity class-->2<mapping resource= "Model/card.hbm.xml"/>3<mapping resource= "Model/ Person.hbm.xml "/>

In Person.hbm.xml, the following code is used to specify the relationship between the person (party) and the card (multiparty), where inverse is true at the end of the person.

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

In Hibernatemain.java, a person's information is inserted through the following key code.

1//Create a card 2Card Card1 = new Card (), 3        card1.setcardid ("Card123"), 4        Card1.setpersonid ("Person123"); 5        Card1.setbank ("Citi"); 6        card1.setbalance (100f); 7//Initializes a person with 8 persons people        = New Man (); 9        Person.setid ( "Person123"),        person.setname ("Peter"),        person.setphone ("123456"), 12//add cards set in card113        <Card> cards = new hashset<card> ();        Cards.add (card1); person.setcards        (cards); 16//Depositary Information 17        Session.save (person);        Session.flush ();

After running, in the output information can see the following two related INSERT statements: One is to insert the person information, and the other is to insert card information.

1 Hibernate:insert into person (Name, Phone, ID) VALUES (?,?,?)

2 Hibernate:insert into Card (PersonID, Bank, Balance, person, CardID) VALUES (?,?,?,?,?)

When you set the inverse in the person.hbm.xml to False, you can see an UPDATE statement in the related statement.

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 setting inverse to true, the person side reverses the foreign key control, that is, the card to manage the foreign key, and in the code we just inserted the person, not inserted card, So there is no update on the operation of two foreign keys (PersonID and Cardid). Conversely, when inverse is false, the management of the foreign key control is on the person side, and hibernate requires an additional UPDATE statement to manage the foreign key when inserting the person.

In a one-to-many case, inverse no matter what the value of the results have no effect, so it is easy to ignore its role.

In a one-to-many example, the general is to allow the multi-party to manage the foreign key control, such as a person has 100 open, then if by someone side to manage, virtually may be more than 100 update operation, the efficiency is not good.

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

Let's take a look at the many-to-many students choosing a course, one of whom can take multiple courses, and a class can have multiple students. In Student.hbm.xml, statements that describe many-to-many relationships can be added with inverse= "true" statements.

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, there is no statement about inverse, that is, to reverse the control of the foreign key at the student end, and turn control over to the course.

In the Hibernatemain class, S1 students take a computer course with the following code.

1//Set up a student, study number is 12Student 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.setcou        Rsename ("Math"), 9 Course c2 = new Course (), C2.setcourseid ("C2"), one C2.setcoursename ("Java"); 12 Course C3 = New Course (), C3.setcourseid ("C3"), C3.setcoursename ("C #"), 15//set computer course this Set16 set <Course> computercourses = new hashset<course> (); Computercourses.add (C2); COMPUTERCOURSES.A        DD (C3); 19//let S1 this student take the computer course (i.e. C2 and C3 course) s1.setcourses (computercourses); 21//save s122 Session.save (S1); 23 Session.flush (); 

The result of the implementation is that while the relevant student and course records can be seen in the student and course tables, there are no associated records in the key Description Student Selection association table (students_courses). The reason is that the foreign key management has been handed over to the course side by setting up the inverse, here is just to save the students, and did not save the course, so there is no action to insert the foreign key. If you want to insert a foreign key association in the Students_courses table, you need to set the value of inverse to false in Person.hbm.xml.

Therefore, in many-to-many associations, setting the wrong inverse value will result in an error, please set the appropriate value according to the situation of the specific project.

  

  

Inverse usage in Hibernate (or other ORM), excerpt from Java Web Lightweight Development Interview tutorial

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.