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);} } Also 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 ( People); 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.