"My OR Mapping Tour" errata and supplement

xiaoxiao2021-03-06  43

"My O / R Mapping Tour (II)", has an analysis of people.hbm.xml, talking about why INVERSE = "True":

In the vehicle management system, it represents a number of vehicles with an owner. Expressed in java.util.set type. INVERSE is used to identify one end of the passive party in the two-way association. Inverse = false side is responsible for maintaining the relationship; in the vehicle management system, AutoInfo acts as the main control, should be set to "True". This is like you (passive One) a lot of cards in a party, but it is possible that you don't know the specific background of the recipient (active MANY); this is not tight, the recipient will contact you when necessary. Active maintenance relationship).

The sentence of the red logo is easy to make people have a discrimination, it seems to be set to "True" with the atuoinfo. Should be changed to:

In the vehicle management system, atuoinfo acts as the main control, INVERSE = "True" should be set in people.

Going down along the idea, you may ask: What is called "active maintenance relationship"? Let's take a look at the code below (taken from

"My O / R Mapping Tour (3)"):

AutoInfo ai = new AutoInfo (); People 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 (person); tx.commit (); session.close ();} catch (exception e) {system.out.println (e);}}

Try the "Ai.SetOwnerno (People)", because AutoInfo has no active maintenance relationship, causing the Owner_no field in the auto_info table to "null". Natural AutoInfo does not have any contacts with Poople.

Human curware is very strong! Why do you have to use AutoInfo as the main control? Does the owner of the PEOPLE? Ok, delete inverse = "true" for people.hbm.xml, run the above program, actually save, just a more SQL: "Update auto_ITO set =?", This is AutoInfo passive Modify contact with people. Exercising more SQL means more overhead, this is unfavorable in performance! "My O / R Mapping Tour (3)", there is a program and description of the second bought car:

AutoInfo ai = new AutoInfo (); People 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 = (person) session .find ("from people where iv_id = 1) .GET (0); ai.setlicenseplate (" A00002 "); Ai.Setownerno ( People); people.getautoinfoset (). Add (ai); session.save (person); tx.commit (); session.close ();} catch (exception e) {system.out.println (e);} }

Here, maybe you will have this idea: "You should be able to insert records directly to the auto_info table, do not turn it through the People object, like writing SQL." Wrong! It used to write SQL directly, but now we use Hibernate, everything must act in an object, see Ai.Setownerno (People)? The incoming parameter is an instance of a People object, not a simple field.

This interpretation is too absolute, in fact, you can save the autoInfo object directly, instead of being transferred by saving people:

AutoInfo ai = new AutoInfo (); People 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 = (person) session .find ("from people where iv_id = 1) .GET (0); ai.setlicenseplate (" A00002 "); Ai.Setownerno ( "session.save (ai); tx.commit (); session.close ();} catch (exception e) {system.out.println (e);}}" My O / R Mapping Tour ( 4) "When deleting the people table and its associated auto_info table, the program is not wrong, but there is a simpler way to delete:

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, you have to do one action, you can have a variety of implementations, which is best, you will rely on yourself.

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and the source:

http://blog.9cbs.net/rosen

)

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

New Post(0)