Parental relationship and inverse details

xiaoxiao2021-03-06  39

First, with the father and child relationship as an example: corresponding to the generated DDL 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;

* Some of the big written inverse = "true" indicates the relationship between the ParentPo itself does not maintain the table! And by the case of Children to maintain, * cascade = "all" means that both UPDATE, INSERT, DELETE keeps a few links * Lazy = "true" means that when the father is initialized, all sons will not bring all their sons from the database. Load comes in. Let's take a look at several examples: Generated SQL: Hibernate: INSERT INTO PARENT (ID) VALUES (DEFAULT)

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default)

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default)

Results C: / MyApp / Sqllib / Bin> DB2 Select * from child ID ParentID

----------- -----------

71 44

72 44

73 44

C: / myapp / sqllib / bin> DB2 Select * from Parent

Id

-----------

44

Note that only one sentence: session.save (PARENT); saves two sons into the database. * First, INVERSE = TRUE: The relationship here is maintained by your son, so if you just join your son, you don't give your son to set your father session.save (PARENT), you will not save your son! Look at this example: Note and Example 1 Contrast * ChildPo Child = New Childpo (PARENT) ---> ChildPo Child = New ChildPo (),

ITXMGR TX = NULL;

TX = HibernatetXMgr.Begintrans ("Add A New Relationships ...");

SESSION = (session) tx.getations ();

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 ( "dddddddddddddddddddddddddddddddddddddddddddddddddddddd"); ChildPO child3 = new ChildPO ();

Child3.SetParent (PARENT);

Session.save (child3);

Session.flush ();

System.out.println ( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");

Tx.endtrans ();

The generated SQL has no hibernate: INSERT INTO PARENT (ID) VALUES (Default)

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default)

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default)

DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDddd

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default)

Eeeeeeeeeeeeeeeeeeeeeeeeeeeee

* Note that the father and child relationship lost C: / myApp / sqllib / bin> db2 select * from child ID ParentID

----------- -----------

74-

75 -

76 45

C: / myapp / sqllib / bin> DB2 Select * from Parent

Id

-----------

45

* Why is the last child's father is not lost? It is child3.setparent (Parent);, the relationship is maintained by the child, if Child does not setparent, or new childpo, the father and child relationship is lost, parent.setchildren (list); is useless! * Here is another problem why it is not very troublesome with inverse? Is it very troublesome to use it? Here, an example gives you an explanation: (Key reason is that the following examples and examples are exactly the same, the difference is no inverse = true example 2:

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;

ITXMGR TX = NULL;

TX = HibernatetXMgr.Begintrans ("Add A New Relationships ...");

SESSION = (session) tx.getations ();

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 ("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDdddddddddd

Childpo child3 = new childpo ();

Child3.SetParent (PARENT);

Session.save (child3);

Session.flush ();

System.out.println ( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");

Tx.endtrans ();

Hibernate generated SQL Hibernate: INSERT INTO PARENT (ID) VALUES (DEFAULT) HIBERNATE: INSERT INTO CHILD (PARENTID, ID) VALUES (?, default)

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default)

Hibernate: Update Child Set ParentId =? Where ID =?

DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDddd

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default) hibernate: value ics identity_val_local ()

Results C: / MyApp / Sqllib / Bin> DB2 Select * from Parent ID

-----------

46

ID ParentID

----------- -----------

77 46

78 46

79 46

It is obviously more than the original hibernate: update child set parentid =? Where id =? For every child, update your father's ID is very slow, because the father has a collection of children, he can't know which child's father ID has been Point to yourself, so for every child, you have to update your father to make him only think of yourself, and this relationship is much more maintenance, each child has only one father, only the settings need to be updated, so obviously, this father and son The relationship is maintained by the child to maintain a relatively labor-saving. Reduce the burden of the database * Now let's take a look at the condition without inverse = TRUE. Childpo child = new childpo (parent) ---> childpo child = new childPo (), ITXMGR TX = NULL;

TX = HibernatetXMgr.Begintrans ("Add A New Relationships ...");

SESSION = (session) tx.getations ();

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 ("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDdddddddddd

Childpo child3 = new childpo ();

Child3.SetParent (PARENT);

Session.save (child3);

Session.flush ();

System.out.println ( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");

Tx.endtrans ();

The resulting SQL and the results and the same Hibernate generated SQL Hibernate: INSERT INTO PARENT (ID) VALUES (DEFAULT) VALUES (?, Default) VALUES

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default)

Hibernate: Update Child Set ParentId =? Where ID =?

DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDddd

Hibernate: INSERT INTO CHILD (PARENTID, ID) VALUES (?, Default)

Hibernate: value identity_val_local () results C: / myapp / sqllib / bin> db2 select * from child ID ParentID

----------------

83 48

84 48

85 48

C: / myapp / sqllib / bin> DB2 Select * from Parent ID

---

48

* Obviously without inverse = true, the father and son must maintain the father and child relationship, so as long as there is a parent.setchildren (), or the child.setParent () is a summary of Inverse = True: No inverse = TURE For the developer to write the code is relatively convenient, but the efficiency of the program execution is low, and it must be noted with inverse = Ture. Be sure to call one party of the maintenance relationship, otherwise it will not intentionally destructive.

转载请注明原文地址:https://www.9cbs.com/read-66548.html

New Post(0)