My O/R Mapping journey; errata and supplement

Source: Internet
Author: User
In my o/R Mapping journey (II), I analyzed people. HBM. xml and talked about why inverse = "true ":

In the vehicle management system, an owner owns multiple vehicles. It is represented by Java. util. Set. Inverse is used to identify the passive end of a bidirectional Association. The inverse = false Party (main prosecution) is responsible for maintaining the association relationship;In the vehicle management system, autoinfo should be set as "true" as the primary control ".This is like you (passive party one) Distributing many business cards at a party, but you may not know the specific background of the recipient (active party); it doesn't matter, the recipient will contact you when necessary (actively maintain the relationship ).

Sentences marked in red are prone to inconsistencies. It seems that atuoinfo is set to "true. Should be changed:

In the vehicle management system, atuoinfo is the primary control, and inverse = "true" should be set in people ".

You may ask: What is "active relationship maintenance "? Take a look at the following code (from my o/R Mapping journey (3):

Autoinfo AI = new autoinfo ();
People = new people ();
Public void dotest (){
Try {
Configuration CFG = new configuration (). Configure ();
Sessionfactory sessions = cfg. buildsessionfactory ();
Session session = sessions. opensession ();
Transaction Tx = session. begintransaction ();

AI. setlicenseplate ("a00001 ");
AI. setownerno (people );
People. setaddress ("China ");
People. setname ("Zhang San ");
People. addtoautoinfoset (AI );
Session. Save (people );
TX. Commit ();
Session. Close ();
} Catch (exception e ){
System. Out. println (E );
}
}

I tried to annotate "ai. setownerno (people)". Because autoinfo does not actively maintain the relationship, the owner_no FIELD IN THE auto_info table is "null ". Naturally, there is no connection between autoinfo and poople.

Human thirst for knowledge is intense!
Why do we have to use autoinfo as the master prosecution? Why can't people take charge of the prosecution? Okay, you can delete inverse = "true" for people. HBM. XML, and then run the above program. It can also be saved, but there is an additional SQL statement: "Update auto_info set owner_no =? Where auto_id = ?", This is how autoinfo passively modifies the contact with people. Executing one SQL statement at a time means more overhead, which is detrimental to performance!

My O/R Mapping trip (iii) has a procedure and description for Michael's second car purchase:

Autoinfo AI = new autoinfo ();
People = new people ();
Public void dotest (){
Try {
Configuration CFG = new configuration (). Configure ();
Sessionfactory sessions = cfg. buildsessionfactory ();
Session session = sessions. opensession ();
Transaction Tx = session. begintransaction ();
People =
(People) session
. Find (
"From people where owner_id = 1 ")
. Get (0 );
AI. setlicenseplate ("a00002 ");
AI. setownerno (people );
People. getautoinfoset (). Add (AI );
Session. Save (people );
TX. Commit ();
Session. Close ();
} Catch (exception e ){
System. Out. println (E );
}
}

Here, you may have the idea: "You should be able to insert records directly to the auto_info table without passing through the people object, which is as easy as writing SQL ." Wrong! In the past, it was possible to directly write SQL statements, but now we use hibernate. Everything must be done with objects. Have you seen ai. setownerno (people? The input parameter is a people object instance, not a simple field.

This explanation is too absolute. In fact, you can directly Save the autoinfo object, instead of saving people:

Autoinfo AI = new autoinfo ();
People = new people ();
Public void dotest (){
Try {
Configuration CFG = new configuration (). Configure ();
Sessionfactory sessions = cfg. buildsessionfactory ();
Session session = sessions. opensession ();
Transaction Tx = session. begintransaction ();
People =
(People) session
. Find (
"From people where owner_id = 1 ")
. Get (0 );
AI. setlicenseplate ("a00002 ");
AI. setownerno (people );
Session. Save (AI );
TX. Commit ();
Session. Close ();
} Catch (exception e ){
System. Out. println (E );
}
}

In my o/R Mapping (4), when deleting the people table and its associated auto_info table, there is no error in the program, but there is a simpler way to delete it:

Try {
Configuration CFG = new configuration (). Configure ();
Sessionfactory sessions = cfg. buildsessionfactory ();
Session session = sessions. opensession ();
Transaction Tx = session. begintransaction ();
Session. Delete ("from people where owner_id = 1 ");
TX. Commit ();
Session. Close ();
} Catch (exception e ){
System. Out. println (E );
}

In hibernate, there are multiple implementation methods to complete an operation. Which of the following is the best? You have to decide on your own.

(Please note! This document shall indicate the original author Rosen Jiang and its source:Http://blog.csdn.net/rosen)

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.