1. Create project, project name hibernatedemo27, directory structure
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8F/8C/wKiom1jkmXSCMMp_AAAitg7MI3I145.png-wh_500x0-wm_ 3-wmp_4-s_3194448875.png "title=" Qq20170405151409.png "alt=" Wkiom1jkmxscmmp_aaaitg7mi3i145.png-wh_50 "/>
2. Create a Lib directory storage jar file in the project, directory structure
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8F/8A/wKioL1jkmaLT3QKVAABcnV7s5Y8797.png-wh_500x0-wm_ 3-wmp_4-s_2834369578.png "title=" Qq20170405151453.png "alt=" Wkiol1jkmalt3qkvaabcnv7s5y8797.png-wh_50 "/>
3. Create the Entity Class Forum, package name (Com.mycompany.demo.bean) in the SRC directory,
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8F/8A/wKioL1jkmcXTJ3ApAAA6KbYpcIs537.png-wh_500x0-wm_ 3-wmp_4-s_749745342.png "title=" Qq20170405151528.png "alt=" Wkiol1jkmcxtj3apaaa6kbypcis537.png-wh_50 "/>
4. The content of the entity forum is as follows
Package Com.mycompany.demo.bean;import Java.util.set;public class Forum {private int fid;private String name;private Set <ForumPost> forumposts;public int Getfid () {return FID;} public void Setfid (int fid) {this.fid = FID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public set<forumpost> getforumposts () {return forumposts;} public void Setforumposts (set<forumpost> forumposts) {this.forumposts = forumposts;}}
5. Create the Entity Class Forum mapping file in the SRC directory Forum.hbm.xml, package name (Com.mycompany.demo.bean),
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/8A/wKioL1jkmfrAqlauAAA83qCeN5k428.png-wh_500x0-wm_ 3-wmp_4-s_4102970537.png "title=" Qq20170405151621.png "alt=" Wkiol1jkmfraqlauaaa83qcen5k428.png-wh_50 "/>
6. The contents of the mapping file Forum.hbm.xml are as follows
<?xml version= "1.0" encoding= "Utf-8"? ><! doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd//en" "HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD" > <!--Package: Specify <class/> packages to be located -->
7. Create the entity class Forumpost, package name (Com.mycompany.demo.bean) in the SRC directory,650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/8F/8D/wKiom1jkmjKS7jhOAAA8wb7zbuw834.png-wh_500x0-wm_ 3-wmp_4-s_2528586986.png "title=" Qq20170405151718.png "alt=" Wkiom1jkmjks7jhoaaa8wb7zbuw834.png-wh_50 "/>
8. The contents of the entity class Forumpost are as followsPackage Com.mycompany.demo.bean;public class Forumpost {private int pid;private String subject;private Forum Forum; public int getpid () {return PID;} public void setpid (int pid) {this.pid = pid;} Public String Getsubject () {return subject;} public void Setsubject (String subject) {this.subject = subject;} Public forum Getforum () {return forum;} public void Setforum (Forum forum) {this.forum = Forum;}}
9. Create the Entity Class Forumpost mapping file ForumPost.hbm.xml, package name (Com.mycompany.demo.bean) in the SRC directory,650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8F/8D/wKiom1jkml_CnoDmAAA8-lI1VNI854.png-wh_500x0-wm_ 3-wmp_4-s_275966837.png "title=" Qq20170405151803.png "alt=" Wkiom1jkml_cnodmaaa8-li1vni854.png-wh_50 "/>
10. The contents of the mapping file ForumPost.hbm.xml are as follows<?xml version= "1.0" encoding= "Utf-8"? ><! doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd//en" "HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD" > <!--Package: Specify <class/> packages to be located -->
11. Create the tool class Hbnutil, package name (com.mycompany.demo.util) in the SRC directory,650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8F/8D/wKiom1jkmqDgWi7hAAAwKdpsq7k363.png-wh_500x0-wm_ 3-wmp_4-s_3698412222.png "title=" Qq20170405151908.png "alt=" Wkiom1jkmqdgwi7haaawkdpsq7k363.png-wh_50 "/>
12. The contents of the tool class Hbnutil are as followsPackage Com.mycompany.demo.util;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public class Hbnutil {private static sessionfactory sessionfactory;public static Session getsession () {if (sessionfactory = = NULL | | sessionfactory.isclosed ()) {sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();} return sessionfactory.getcurrentsession ();}}
13. Create the Hibernate profile Hibernate.cfg.xml in the SRC directory,650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8F/8A/wKioL1jkms-wm5-sAAAsPdiow7Y256.png-wh_500x0-wm_ 3-wmp_4-s_2049828931.png "title=" Qq20170405151955.png "alt=" Wkiol1jkms-wm5-saaaspdiow7y256.png-wh_50 "/>
The contents of the 14.Hibernate configuration file Hibernate.cfg.xml are as follows<?xml version= "1.0" encoding= "Utf-8"? ><! doctype hibernate-configuration system "http://www.hibernate.org/dtd/ Hibernate-configuration-3.0.dtd ">
15. Create the test directory in the project to store testing files, file name TestApp, package name (Com.mycompany.demo.bean), directory structure650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8F/8A/wKioL1jkmv2iIaWJAAAp9GMgykQ500.png-wh_500x0-wm_ 3-wmp_4-s_2691096905.png "title=" Qq20170405152043.png "alt=" Wkiol1jkmv2iiawjaaap9gmgykq500.png-wh_50 "/>
The contents of the 16.TESTAPP test class are as followspackage com.mycompany.demo.bean;import java.util.hashset;import java.util.list;import java.util.set;import org.hibernate.criteria;import org.hibernate.session;import org.hibernate.sql.jointype;import org.junit.before;import org.junit.test;import com.mycompany.demo.util.hbnutil;public class testapp {private session session;@ Beforepublic void init () {session = hbnutil.getsession ();} /* * one-to-many bidirectional association-add * need to set the Set property in Forum.hbm.xml to Cascade= "All" */@Testpublic void testonetomanyadd () {try {session.begintransaction (); Forumpost forumpost1 = new forumpost (); Forumpost1.setsubject ("A"); Forumpost forumpost2 = new forumpost (); Forumpost2.setsubject ("B"); Set<forumpost> forumposts = new hashset<forumpost> (); ForumPosts.add (ForumPost1 ); Forumposts.add (FORUMPOST2); Forum forum = new forum (); Forum.setname ("Foruma"); forum.setforumposts (forumposts); Session.save (forum); Session.gettransaction (). Commit () ;} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * Many-to-one bidirectional association-fetch= "select", lazy= "false" */@Testpublic void testfetchforselect () {try {session.begintransaction (); Forumpost forumpost = session.get (FORUMPOST.CLASS,&NBSP;10); Forum forum = forumpost.getforum (); System.out.println (Forum.getfid ()); System.out.println (Forum.getname ()); Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}}
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8F/8A/wKioL1jkmxyzgWTGAABwCKMO5XE595.png-wh_500x0-wm_ 3-wmp_4-s_57527261.png "title=" Qq20170324102905.png "alt=" Wkiol1jkmxyzgwtgaabwckmo5xe595.png-wh_50 "/>
This article from the "Vegetarian Yan" blog, declined to reprint!
hibernate5--to 1 (n:1)-fetch= "select"-lazy= "false"