Reference: http://hi.baidu.com/ohaozy/blog/item/ed486a33bc83cf48ac4b5f75.html
Below is the code I have debugged
A simple example is provided to illustrate these relationships: The following tests are completed in mysql5. the database table is configured in hibernate. cfg. xml. One-to-learn and others-to-one examples show the relationship between teachers and students. A teacher corresponds to multiple students, and multiple students correspond to one teacher. |
- Create class
Teacher. Java
Package COM. SJTU. XW. pojo; <br/> Import Java. util. hashset; <br/> Import Java. util. set; <br/> public class teacher {<br/> private long ID; <br/> private string teaname; <br/> private set students = new hashset (); <br/> Public long GETID () {<br/> return ID; <br/>}< br/> Public void setid (long ID) {<br/> This. id = ID; <br/>}< br/> Public String getteaname () {<br/> return teaname; <br/>}< br/> Public void setteaname (string teaname) {<br/> This. teaname = teaname; <br/>}</P> <p> Public set getstudents () {<br/> return students; <br/>}< br/> Public void setstudents (set students) {<br/> This. students = students; <br/>}< br/> Public String tostring () {<br/> return "Teacher: [teacherid =" + this. ID + "/tteachername =" <br/> + this. teaname + "]"; <br/>}< br/>
Student. Java
Package COM. SJTU. XW. pojo; <br/> public class student {<br/> private long ID; <br/> private string stuname; <br/> Public long GETID () {<br/> return ID; <br/>}< br/> Public void setid (long ID) {<br/> This. id = ID; <br/>}< br/> Public String getstuname () {<br/> return stuname; <br/>}< br/> Public void setstuname (string stuname) {<br/> This. stuname = stuname; <br/>}< br/> Public String tostring () {<br/> return "Student: [studentid =" + this. ID + "/tstudentname =" <br/> + this. stuname + "]"; <br/>}< br/>
Student. HBM. xml
<? XML version = "1.0"?> <Br/> <! Doctype hibernate-mapping Public <br/> "-// hibernate/hibernate DTD ing DTD 3.0/EN" <br/> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <br/> <pibernate-mapping package = "com. SJTU. XW. pojo "> <br/> <class name =" student "table =" student "> <br/> <ID name =" ID "column =" ID "> <br/> <generator class = "native"> </generator> <br/> </ID> <br/> <property name = "stuname" column = "studentname" length =" 30 "/> <br/> </class> <br/> </pibernate-mapping>
Teacher. HBM. xml
<? XML version = "1.0"?> <Br/> <! Doctype hibernate-mapping Public <br/> "-// hibernate/hibernate DTD ing DTD 3.0/EN" <br/> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <br/> <pibernate-mapping package = "com. SJTU. XW. pojo "> <br/> <class name =" teacher "table =" teacher "> <br/> <ID name =" ID "column =" ID "> <br/> <generator class = "native"> </generator> <br/> </ID> <br/> <property name = "teaname" column = "teachername" length =" 30 "/> </P> <p> <set name = "Students" Fetch = "join" lazy = "false"> <br/> <! -- <Br/> <set name = "Students"> <br/> --> <br/> <key column = "teacherid"> </key> <br/> <! -- <Br/> column = "teacherid" indicates the name of a teacherid column in the student table, is the foreign key pointing to the teacher table <br/> --> <br/> <one-to-learn class = "student"/> <br/> </set> <br /> </class> <br/> </pibernate-mapping>
Hibernate. cfg. xml
<? XML version = '1. 0' encoding = 'utf-8'?> <Br/> <! Doctype hibernate-configuration Public <br/> "-// hibernate/hibernate configuration DTD 3.0/EN" <br/> "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <br/> <pibernate-configuration> <br/> <session-factory> <br/> <! -- Database connection settings --> <br/> <property name = "connection. driver_class "> COM. mySQL. JDBC. driver </property> <br/> <property name = "connection. URL "> JDBC: mysql: // localhost: 3306/coursems </property> <br/> <property name =" connection. username "> root </property> <br/> <property name =" connection. password "> root </property> <br/> <property name =" dialect "> Org. hibernate. dialect. mysqldialect </property> <br/> <property name = "cache. provider_class "> Org. hibernate. cache. nocacheprovider </property> <br/> <property name = "show_ SQL"> false </property> <br/> <Mapping Resource = "com/SJTU/XW/pojo/student. HBM. XML "/> <br/> <Mapping Resource =" com/SJTU/XW/pojo/teacher. HBM. XML "/> <br/> </session-factory> <br/> </pibernate-configuration>
Hibernateutil. Java
Package COM. SJTU. XW. util; </P> <p> Import Org. hibernate. sessionfactory; <br/> Import Org. hibernate. cfg. configuration; <br/> public class hibernateutil {<br/> Private Static final sessionfactory = buildsessionfactory (); <br/> Private Static sessionfactory buildsessionfactory () {<br/> try {<br/> // create the sessionfactory from hibernate. cfg. XML <br/> return new configuration (). configure (). buildsessionfactory (); <br/>}< br/> catch (throwable ex) {<br/> // make sure you log the exception, as it might be swallowed <br/> system. err. println ("Initial sessionfactory creation failed. "+ ex); <br/> throw new exceptionininitializererror (Ex); <br/>}< br/> Public static sessionfactory getsessionfactory () {<br/> return sessionfactory; <br/>}< br/>}
Test. Java
Package COM. SJTU. XW. test; <br/> Import Java. util. list; <br/> Import Org. hibernate. session; <br/> Import Org. hibernate. sessionfactory; <br/> Import Org. hibernate. transaction; <br/> Import COM. SJTU. XW. pojo. student; <br/> Import COM. SJTU. XW. pojo. teacher; <br/> Import COM. SJTU. XW. util. hibernateutil; <br/> public class test {<br/> Public static void main (string [] ARGs) <br/>{< br/> test = new test (); <br/> // t EST. addstudent (); <br/> // test. addteacher (); <br/> test. test (); <br/>}< br/> Public void addstudent () {<br/> Student = new student (); <br/> student. setstuname ("student6"); <br/> sessionfactory Sf = hibernateutil. getsessionfactory (); <br/> session = SF. opensession (); <br/> transaction Tx = session. begintransaction (); <br/> session. save (student); <br/> Tx. commit (); <br/> session. close (); <br/> SF. close (); <br/>}< br/> Public void addteacher () {<br/> Student = new student (); <br/> student. setid (3); <br/> Teacher = new teacher (); <br/> teacher. setteaname ("teacher1"); <br/> teacher. getstudents (). add (student ); // Add students with ID 1 to the instructor object <br/> // when saving the instructor, the student table is updated <br/> sessionfactory Sf = hibernateutil. getsessionfactory (); <br/> session = SF. opensession (); <br/> transaction Tx = s Ession. begintransaction (); <br/> session. save (teacher); <br/> Tx. commit (); <br/> session. close (); <br/> SF. close (); <br/>}< br/> Public void test () <br/>{< br/> sessionfactory Sf = hibernateutil. getsessionfactory (); <br/> session = SF. opensession (); <br/> transaction Tx = session. begintransaction (); </P> <p> List list = session. createquery ("from teacher t where T. id = 1 "). list (); <br/> // list = s Ession. createquery ("from teacher T left join fetch T. students where T. id = 1 "). list (); </P> <p> teacher t = (teacher) list. get (0); </P> <p> system. out. println (t); // <br/> system. out. println (T. getstudents (). iterator (). next (); <br/> for (INT I = 0; I <list. size (); I ++) <br/>{< br/> teacher TT = (teacher) list. get (I); <br/> system. out. println (TT); // <br/> system. out. println (TT. getstudents (). iterator (). next (); <B R/>}</P> <p>/* // What does it mean? What is the relationship with the session? <Br/> * before the session ends, student will be automatically queried when used. Otherwise, the query statement should be changed: <br/> * from teacher T left join fetch T. students where T. id = 1 <br/> * or set teacher. HBM. add the set attribute in XML with: Fetch = "join" lazy = "false ": <br/> * <set name = "Students" Fetch = "join" lazy = "false"> <br/> * <key column = "teacherid"> </key> <one-to-define class = "model. student "/> </set> <br/> */<br/> Tx. commit (); <br/> session. close (); <br/> SF. close (); <br/>}< br/>}