MANY-TO-One relationship
Bromon original, please respect the copyright
It is very clear that a pair or more of a relationship is a very common phenomenon in the relational database. Below the father - son's example to demonstrate a couple, more on a relationship is similar, but in our example, it is not advisable. Otherwise, it will bring ethical problems.
First define Child class: / * * Created on 2004-5-8 * / package org.bromon.zizz;
/ ** * @Author bromon * / public class child {private int id; private string name; private int fitherid; private person fa;
Public child () {} / ** * @Return * / public person getfather () {returnaf;
/ ** * @Return * / public int getfatherid () {returnialid;}
/ ** * @Return * / public int getId () {return;}
/ ** * @Return * / public string getname () {return name;}
/ ** * @Param person * / public void setfather (Person P) {Father = P;}
/ ** * @Param i * / public void setfatherId (int i) {fatherid = i;}
/ ** * @Param i * / public void setid (INT i) {id = i;}
/ ** * @Param string * / public void setname (String string) {name = string;
}
The FatherId here is the foreign key, associating the ID field of the Person table.
Here is the mapping file child.hbm.xml: XML Version = "1.0"?> http: / /Hibernate.SourceForge.Net/Hibernate-mapping-2.0.dtd ">
Import java.util. *;
Private set children = new hashset (); / ** * @Return * / public set getchildren () {return children;} / ** @Param set * / public void setchildren (set set) {children = set;
Then modify the Person.hbm.xml, map the added code:
The key column here is the foreign key of the Child table, which needs to be specified as TRUE.
Let's do this, the function is to query the record of id = 1 in the Person table, as a child's father, then add a new record to the Child table.
/ * * CREATED ON 2004-5-8 * / package org.bromon.zizz; import net.sf.hibernate. *; Import net.sf.hibernate.cfg. *; Import net.sf.hibernate.tool.hbm2ddl. *; / ** * @Author bromon * / public class operatechild {/ ** * @Param args * / public static void main (string args []) {try {configure cfg = new configuration (). Addclass (person.class ); Cfg.addclass; sessionfactory sessions = cfg.buildsessionFactory (); new schemaExport (CFG) .create (true, true); session session = sessions.opensession (); child c = new child (); / * Query q = session.createQuery ("from org.brom.zizz.Person as p where p.id = 1); Person P = (Person) q.list (). Get (0); * / Person P = (Person) session.find ("from org.brom.zizz.Person as p where p.id =?", New integer (1), hibernate.integer) .GET (0); system.out.println (p .getname ()); C.setname ("andy"); C.setfather (p); Transaction TS = session.begintransaction (); session.save (c); ts.commit (); session.close (); } ATCH (Exception E) {system.out.println (e);}}} The part of the annotated is another Query method of HQL. In this example, you can see that the object's query is very easy, you don't need you to pack data, modify, and delete objects.
// Get an object Query Q = session.createQuery ("from org.brom.zizz.Person as p where p.id = 1"); Person P = (Person) q.list (). Get (0);
// Modify the data P.setname ("MR Smith");
/ / Save data session.save (p); session.flush ();
// Delete data session.delete (p); session.flush ();