Inverse in hibernate

Source: Internet
Author: User

And Inverse are the basic concepts in hibernate bidirectional relationships. The true role of inverse is to specify which Party maintains the association. If "inverse = false" is specified in one Party (default), that party is responsible for the relationship between them, to put it bluntly, how does hibernate generate SQL statements to maintain the associated records? Hibernate only updates the database synchronously according to the state changes of the master control object. According to the original ing file, people. getAddresses (). add (address), that is, the status of the master control object has changed, so the database will update the database along with the changes in the object status; and address. setPeople (people), that is, the state of the object to be prosecuted has changed. It cannot trigger the simultaneous update of the object and the database. (Instance 1): the simplest one-to-many parent-child relationship. The code is written as follows: parent relationship ing {set name = "children" lazy = "true" inverse = "true"} {key column = "parent_id"/} {one-to-learn class =" test. child "/} {/set} son link ing {parent-to-one name =" Parent "column =" parent_id "not-null =" true "/} parent p = new Parent (); child c = new Child (); c. setParent (p); // maintain the relationship between parent and child p. getChildren (). add (c); session. save (p); session. flush (); Note: {failed-to-one} is always set to "inverse = false", and this attribute does not exist in Mapping! The running result is: Hibernate: insert into parent (id) values (?) Hibernate: insert into child (parent_id, id) values (?, ?) If c. setParent (p) is commented out, the result is: Hibernate: insert into parent (id) values (?) ========================================================== ============= (Instance 2): one Person can participate in multiple events, and one Event has multiple persons. The ing file is as follows: <! -- Person. hbm. xml -->

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.