In this section, it is really beginning to operate the Auto_INFO and the PEOPLE table.
To let Hibernate run, you have to know several key objects:
Net.sf.Hibernate.cfg.configuration instance is responsible for managing Hibernate configuration information, such as database connections, database Dialect, and most important mapping file initialization work.
For the SESSION instance, the program must first get its factory net.sf.hibernate.SessionFactory, SessionFactory instance constructed by the Configuration.
Net.sf.hibernate.Session is the foundation of all database operations, like the JDBC's Connection, and the Session instance is obtained by sessionFactory.
The instance of Net.sf.hibernate.TransAction is obtained by the session. Hibernate does not have transaction management capabilities, Hibernate entrusts it to the underlying JDBC or JTA to achieve transaction management and scheduling. JDBC transaction management is used herein.
Take the above knowledge:
Configuration cfg = new configuration (). Configure (); sessionfactory sessions = cfg.buildsessionFactory (); session session = sessions.opensession (); transaction tx = session.begintransaction ();
Configuration cfg = new configuration (). Configure (); sessionfactory sessions = cfg.buildsessionFactory (); session session = sessions.opensession (); transaction tx = session.begintransaction ();
The first scene of the program will appear immediately.
There is a person called Zhang San, he bought a car. Since it is the first time to buy a car, you must perform Insert operation after entering the vehicle management system.
A one-to-many relationship formed is as follows:
Package com.dao; import java.util. *; import net.sf.hib.hibernate. *; import.sf.hibernate.cfg. *; import bo. *; public class test {list list; 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.setaddress ("China"); People.SetName ("Zhang 3"); People.AddtoAutoinfoset (AI); Ai.SetLicenseplate ("A00001"); ai.SetownerNo (people); session .save (people); tx.commit (); session.close ();} catch (exception e) {system.out.println (e);}}} package com.dao; import java.util. *; import Net.sf.hibernate. *; import net.sf.hibagenate.cfg. *; import bo. *; public class test {list list; autoInfo ai = new autoinfo (); people people = new people (); public void dotest () {Try {Configuration CFG = New Configuration (). Configure (); sessionFactory sessions = cfg.buildsessio NFActory (); session session = sessions.opension (); transaction tx = session.begintransaction (); people.setaddress ("China"); People.SetName ("Zhang San"); People.AddtoautoinFoset (AI); AI. SetLicensePlate ("A00001"); ai.SetownerNo (People); session.save (person); tx.commit (); session.close ();} catch (exception e) {system.out.println (e);} }
After setting the automoinfo, the people object property, call the session.save () method to save, and then transaction, and finally close the session. Ok, look at the database, everything has been saved.
The second curtain of the program appeared.
Zhang San came to do business, he managed very well, intended to buy a car journey to transport. For the second bought car, the people of the vehicle management system has already recorded his basic information, and it is not to operate on the PEOPLE table. Just record the auto_info table INSERT.
A one-to-many relationship formed is as follows: package com.dao; import java.util. *; Import net.sf.hibernate. *; Import.sf.hibernate.cfg. *; Import bo . *; public class Test {List list; 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);}}}
Package com.dao; import java.util. *; import net.sf.hib.hibernate. *; import.sf.hibernate.cfg. *; import bo. *; public class test {list list; 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 whired = 1") .GET (0); ai.setlicenseplate ("A00002"); ai.SetOwnerno (people); people.getautoinfoseet () .ADD (AI); session.save (people); tx.commit (); session.close ();} catch (exception e) {system.out.println (e);}}}
The session.find () method returns an instance of a list interface, which is packaged, "from people where owner_id = 1" is a famous HiBernate Query Language. Is it very similar to SQL? There is a multi-relationship between the people table and the auto_info table, which requires the People object to hold multiple autoinfo objects (in instance packages in the SET interface, see the people class source code), and then through People.GetAutoinfoset (). Add (ai) Add a new record for the Auto_Info table. Ok, after execution, check the database. This code
People = (person) session.find ("from people where iv_id = 1"). Get (0);
Can
People = (people) session.load (People.class, New Integer (1));
exchange.
In this case, the list interface example size () is 1, i.e. there is only one PO;
session.low ()
It is only a single one according to the persistent object and the primary key. So the two ways return are the same PO. Which way to use is determined by you.
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.
The third scene of the program appeared.
Oh, this third scene is the easiest, the first one-to-many) query:
Package com.dao; import java.util. *; import net.sf.hib.hibernate. *; import.sf.hibernate.cfg. *; import bo. *; public class test {list list; 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 (); List list = Session.find ("SELECT AI from AutoInfo As ai WHERE AI.OWNERNO.ID = 1"); for (int i = 0; i By the end of the year, check Zhang Sanyi has many cars. "SELECT AI from AutoInfo As ai WHERE AI.OWNERNO.ID = 1", this HQL is looking for AutoInfo objects, and traditional SQL writes: "SELECT P.NAME, P.ADDress, AI.License_Plate from People P, Auto_INFO AI WHERE P.OWNER_ID = 1 ". Since the auto_info table is the "MANY" table, the People table is "one" table, my approach returns a plurality of POs based on the "MANY" table, where the "one" table is held. "Ai.OWNERNO.ID = 1" is to tell the corresponding auto_info table record by the PEOPLE table record of the primary key parameter "1". For one-to-many relationship, there is a second way: Package com.dao; import java.util. *; import net.sf.hib.hibernate. *; import.sf.hib.hibernate.cfg. *; import bo. *; public class test {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 (); people = (people) Session.Load (People.class, New Integer); item itrator = people.getautoinfoset (). Iterator (); system.out.println (people.getname ()); System.get.Println (people.getaddress ()); While (iterator.hasnext ()) {ai = (autoInfo) iterator.next (); system.te.println (ai.getlicenseplate ());} session.close ();} catch (Exception E) {System.out.println (e);}}} Package com.dao; import java.util. *; import net.sf.hib.hibernate. *; import.sf.hib.hibernate.cfg. *; import bo. *; public class test {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 (); people = (people) Session.Load (People.class, New Integer); item itrator = people.getautoinfoset (). Iterator (); system.out.println (people.getname ()); System.get.Println (people.getaddress ()); While (iterator.hasnext ()) {ai = (autoInfo) iterator.next (); system.te.println (ai.getlicenseplate ());} session.close ();} catch (Exception E) {System.out.println (e);}}} people = (person) session.load (person.class, new integer) Removes a single people object, where holds a number of autoinfo objects in SET package, Iterator Iterator = people.getautoinFoset (). Itrator () turns the set to Iterator, and finally uses while (iterator.hasnext ()) to remove the license plate number. The first one-to-many is actually converted into a multi-TO-One. There are two shortcomings after this conversion: 1. When the HQL is turned to SQL output, the number of SQLs that print is more than one-to-many relationship; two, the people of the people have a large amount of redundancy. Yu (only one example is required, and the same instance is taken out). We know that the performance of the database is limited, and the cost of constructors is high, so unnecessary performance overhead should be minimized. Although I personally don't recommend the one-to-many query to convert a Many-TO-ONE query, in fact, some development teams are willing to adopt, even if they know that the performance is slightly reduced. What do you have any views before you haven't had more research? (Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen) (Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)