Parent-child relationship and Inverse

Source: Internet
Author: User
Document directory
  • HTML tags and JavaScript tutorial

HTML tags and JavaScript tutorial


Parent-child relationship and Inverse

Take the parent-child relationship as an example:
<
Corresponding generated DDL drop table parent;

Java code:

Drop Table child;
Create Table parent
(
Id integer not
Null
Generated by default as identity, primary key
(
ID
)
)
;
Create Table child
(
Id integer not
Null
Generated by default as identity, parentid integer, primary key
(
ID
)
)
;
Alter table child add constraint fk3d1fcfc74b18345 foreign key
(
Parentid
)
References parent;

* In upper case, inverse = "true" indicates that parentpo itself does not maintain the relationship between tables !, It is maintained by children,
* Cascade = "all" indicates that no matter whether it is Update, insert, or delete, the connections are maintained.
* Lazy = "true" indicates that no son will be loaded from the database when the father is initialized.
Let's take a look at several examples:
Generated SQL:

Java code:

Hibernate: insert into Parent
(
ID
)
Values
(
Default
)

Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Result c:/MyApp/sqllib/bin> DB2 select * from child

Java code:

Id parentid
----------------------

71

44


72

44


73

44

C:/MyApp/sqllib/bin> DB2 select * from parent
ID
-----------

44

Note that session. Save (parent); saves the two sons to the database.
* First, let's talk about the role of inverse = true: here the relationship is maintained by the son, so if you just add the son to the father and do not set the father for the son, session. save (parent) will not save the son! Take a look at this example: Pay attention to the comparison with example 1.

Java code:

* Childpo child =
New
Childpo
(
Parent
)
---> Childpo child =
New
Childpo
(
)
,
Itxmgr Tx =
Null
;
Tx = hibernatetxmgr.
Begintrans
(
"Add
New
Relationships ..."
)
;
Session =
(
Session
)
TX.
Getsession
(
)
;
Parent =
New
Parentpo
(
)
;
Childpo child =
New
Childpo
(
)
;
Childpo child2 =
New
Childpo
(
)
;

List
List =
New

Arraylist
(
)
;
List.
Add
(
Child
)
;
List.
Add
(
Child2
)
;
Parent.
Setchildren
(
List
)
;
Session.
Save
(
Parent
)
;
Session.
Flush
(
)
;

System
.
Out
.
Println
(
"Dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
)
;
Childpo Child3 =
New
Childpo
(
)
;
Child3.
Setparent
(
Parent
)
;
Session.
Save
(
Child3
)
;
Session.
Flush
(
)
;

System
.
Out
.
Println
(
"Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
)
;
TX.
Endtrans
(
)
;

The generated SQL has not changed

Java code:

Hibernate: insert into Parent
(
ID
)
Values
(
Default
)

Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

* Note that the parent-child relationship is missing C:/MyApp/sqllib/bin> DB2 select * from child

Java code:

Id parentid
----------------------

74
-

75
-

76

45

C:/MyApp/sqllib/bin> DB2 select * from parent
ID
-----------

45

* Why did the father of the last child not lose? It is child3.setparent (parent);, so the relationship is maintained by the child. If the child does not setparent or new childpo (father), the parent-child relationship will be lost, parent. setchildren (list); is useless!
* Here I introduced another question: Why should I use inverse? Is it difficult to use it to maintain relationships ?, Here is an example to explain: (the key reason is performance)
The following example is exactly the same as the example. The difference is that inverse = true is not used.
Example 2:

Java code:

Hibernate-mapping>
<
Class
Name = "com.
Etech
.
BM
.
Po
.
Childpo
"Table =" child ">
<ID name = "ID" column = "ID" type = "integer">
<Generator
Class
= "Identity"/>
</ID>
<Role-to-one name = "parent"
Class
= "Com.
Etech
.
BM
.
Po
.
Parentpo
"Column =" parentid "/>
</
Class
>
<
Class
Name = "com.
Etech
.
BM
.
Po
.
Parentpo
"Table =" parent ">
<ID name = "ID" column = "ID" type = "integer">
<Generator
Class
= "Identity"/>
</ID>
<Bag name = "children" cascade = "all">
<Key column = "parentid"/>
<One-to-least
Class
= "Com.
Etech
.
BM
.
Po
.
Childpo
"/>
</Bag>
</
Class
>
</Hibernate-mapping>

Java code:

Drop table parent;
Drop Table child;
Create Table parent
(
Id integer not
Null
Generated by default as identity, primary key
(
ID
)
)
;
Create Table child
(
Id integer not
Null
Generated by default as identity, parentid integer, primary key
(
ID
)
)
;
Alter table child add constraint fk3d1fcfc74b18345 foreign key
(
Parentid
)
References parent;

Java code:

Itxmgr Tx =
Null
;
Tx = hibernatetxmgr.
Begintrans
(
"Add
New
Relationships ..."
)
;
Session =
(
Session
)
TX.
Getsession
(
)
;
Parent =
New
Parentpo
(
)
;
Childpo child =
New
Childpo
(
Parent
)
;
Childpo child2 =
New
Childpo
(
Parent
)
;

List
List =
New

Arraylist
(
)
;
List.
Add
(
Child
)
;
List.
Add
(
Child2
)
;
Parent.
Setchildren
(
List
)
;
Session.
Save
(
Parent
)
;
Session.
Flush
(
)
;

System
.
Out
.
Println
(
"Dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
)
;
Childpo Child3 =
New
Childpo
(
)
;
Child3.
Setparent
(
Parent
)
;
Session.
Save
(
Child3
)
;
Session.
Flush
(
)
;

System
.
Out
.
Println
(
"Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
)
;
TX.
Endtrans
(
)
;

Hibernate-generated SQL hibernate: insert into parent (ID) values (default)

Java code:

Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Hibernate: Update Child set parentid =? Where id =?
Dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Hibernate: Values identity_val_local
(
)

Result c:/MyApp/sqllib/bin> DB2 select * from parent

Java code:

ID
-----------

46

Id parentid
----------------------

77

46


78

46


79

46

Obviously, there is one more hibernate: Update Child set parentid =? Where id =? Updating father IDs for every child is obviously slow. Because a father has a collection of children, he cannot know which child's father ID has pointed to himself. Therefore, for every child, updating his father makes him only want to be himself, and the relationship is much better maintained by the Child. Every child has only one father and needs to be updated only after being set. Therefore, obviously, this parent-child relationship is effort-saving for children. reduces the burden on the database
* Now let's take a look at childpo child = new childpo (parent) ---> childpo child = new childpo () without inverse = true (),

Java code:

Itxmgr Tx =
Null
;
Tx = hibernatetxmgr.
Begintrans
(
"Add
New
Relationships ..."
)
;
Session =
(
Session
)
TX.
Getsession
(
)
;
Parent =
New
Parentpo
(
)
;
Childpo child =
New
Childpo
(
)
;
Childpo child2 =
New
Childpo
(
)
;

List
List =
New

Arraylist
(
)
;
List.
Add
(
Child
)
;
List.
Add
(
Child2
)
;
Parent.
Setchildren
(
List
)
;
Session.
Save
(
Parent
)
;
Session.
Flush
(
)
;

System
.
Out
.
Println
(
"Dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
)
;
Childpo Child3 =
New
Childpo
(
)
;
Child3.
Setparent
(
Parent
)
;
Session.
Save
(
Child3
)
;
Session.
Flush
(
)
;

System
.
Out
.
Println
(
"Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
)
;
TX.
Endtrans
(
)
;

The generated SQL and result are the same as the above SQL hibernate generated hibernate: insert into parent (ID) values (default)

Java code:

Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Hibernate: Update Child set parentid =? Where id =?
Dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
Hibernate: insert into child
(
Parentid, ID
)
Values
(
?, Default
)

Hibernate: Values identity_val_local
(
)

Result c:/MyApp/sqllib/bin> DB2 select * from child

Java code:

Id parentid
----------------------

83

48


84

48


85

48

C:/MyApp/sqllib/bin> DB2 select * from parent

Java code:

ID
-----------

48

* Obviously, the Parent and Child sides maintain the parent-child relationship without inverse = true. Therefore, you only need to have parent. setchildren () or child. setparent ().
Summary of inverse = true: it is easier for developers to write code without inverse = ture, but the program execution efficiency is low. Be sure to use inverse = ture, be sure to call the party that maintains the relationship, otherwise it will have unexpected destructive power.
 

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.