Thoroughly understand the inverse (2)-zz in hibernate

Source: Internet
Author: User
In most cases, "inverse = false" is rarely used, but I still want to study it carefully (so that I can better understand "inverse = true "). Pai_^ Why is "inverse = false" rarely used "? The reason is very simple as mentioned earlier, that is, the two-way relationship will not be optimized. Let's look at a parent-child example: Relationship ing in the parent
{Set name = children lazy = true inverse = false}
{Key column = parent_id /}
{One-to-learn class = test. Child /}
Link ing in {/set} son
{Role-to-one name = parent column = parent_id not-null = true/} code: parent P = new parent ();
Child C = new child ();
P. getchildren (). Add (C );
C. setparent (p); Session. Save (P );
Session. Save (C );
Session. Flush (); Result: hibernate: insert into parent (ID) values (?)
Hibernate: insert into child (parent_id, ID) values (?, ?)
Hibernate: Update Child set parent_id =? Where id =? Here we should understand why "inverse = true" will optimize the SQL statement! In addition, we also mentioned that "inverse = false" is used to maintain the relationship, which is maintained by the father. Suppose I drop flush () between SAVE (P) and save (c). What is the result? In fact, we cannot do this! Because my father is responsible for maintaining the relationship, if I add flush () between them, then the association update will not be available (my father needs a persistent son to trigger the association update). Next let's take a look at the update: parent P = (parent) session. load (parent. class, parentid );
Parent P2 = (parent) Session. Load (parent. Class, parentid2 );

C = (child) Session. Find (
"From child as child where child. Parent =? ",
P, hibernate. Entity (parent. Class). Get (0); p. getchildren (). Remove (C );
P2.getchildren (). Add (C );
C. setparent (P2); Result: hibernate: Select parent0 _. ID as ID from parent parent0 _ Where parent0 _. ID =? // Get parent 1
Hibernate: Select parent0 _. ID as ID from parent parent0 _ Where parent0 _. ID =? // Get parent 2
Hibernate: Select child0 _. ID as ID, child0 _. parent_id as parent_id from child child0 _ Where (child0 _. parent_id =? )
// Get first child for parent 1 hibernate: Select child0 _. ID as ID __, child0 _. ID as ID, child0 _. parent_id as parent_id from child child0 _ Where child0 _. parent_id =?
Hibernate: Select child0 _. ID as ID __, child0 _. ID as ID, child0 _. parent_id as parent_id from child child0 _ Where child0 _. parent_id =? Hibernate: Update Child set parent_id =? Where id =? // Child. setparent
Hibernate: Update Child set parent_id = NULL where parent_id =? // Remove
Hibernate: Update Child set parent_id =? Where id =? // The add result indicates that when "inverse = false" is set, the relationship is maintained by the father and son. This efficiency is much lower than the "inverse = true" I mentioned earlier. The relationship was maintained by my father. I changed the code again: parent P = (parent) Session. Load (parent. Class, parentid );
Parent P2 = (parent) Session. Load (parent. Class, parentid2 );

C = (child) Session. Find (
"From child as child where child. Parent =? ",
P, hibernate. entity (parent. class )). get (0); p2.getchildren (). add (c); Result: hibernate: Select parent0 _. ID as ID from parent parent0 _ Where parent0 _. id =? // Get parent 1
Hibernate: Select parent0 _. ID as ID from parent parent0 _ Where parent0 _. ID =? // Get parent 2
Hibernate: Select child0 _. ID as ID, child0 _. parent_id as parent_id from child child0 _ Where (child0 _. parent_id =? )
// Get first child for parent 1 hibernate: Select child0 _. ID as ID __, child0 _. ID as ID, child0 _. parent_id as parent_id from child child0 _ Where child0 _. parent_id =?
Hibernate: Select child0 _. ID as ID __, child0 _. ID as ID, child0 _. parent_id as parent_id from child child0 _ Where child0 _. parent_id =? Hibernate: Update Child set parent_id =? Where id =? // Add is correct at first glance, but the children statuses of Father P and P2 are inconsistent! Conclusion: "inverse = true" can be used to optimize the code. We recommend that you use "inverse = true" when establishing a bidirectional relationship "!

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.