1. Create project, project name hibernatedemo23, directory structure
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/8F/88/wKiom1jkS66ixaaQAAAhKCFsVQ8175.png-wh_500x0-wm_ 3-wmp_4-s_1548641453.png "title=" Qq20170405094219.png "alt=" Wkiom1jks66ixaaqaaahkcfsvq8175.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/M01/8F/88/wKiom1jkS8-y93GzAABa1keXcbc982.png-wh_500x0-wm_ 3-wmp_4-s_1485382156.png "title=" Qq20170405094250.png "alt=" Wkiom1jks8-y93gzaaba1kexcbc982.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://s2.51cto.com/wyfs02/M01/8F/85/wKioL1jkS_fhAu8bAAA3p7x3VWY877.png-wh_500x0-wm_ 3-wmp_4-s_1903337236.png "title=" Qq20170405094331.png "alt=" Wkiol1jks_fhau8baaa3p7x3vwy877.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://s4.51cto.com/wyfs02/M02/8F/85/wKioL1jkTEGC6w7GAAA801ZzDBA497.png-wh_500x0-wm_ 3-wmp_4-s_3593463686.png "title=" Qq20170405094443.png "alt=" Wkiol1jktegc6w7gaaa801zzdba497.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://s2.51cto.com/wyfs02/M00/8F/85/wKioL1jkTJPDqXczAAA81Nah5dc291.png-wh_500x0-wm_ 3-wmp_4-s_886669193.png "title=" Qq20170405094607.png "alt=" Wkiol1jktjpdqxczaaa81nah5dc291.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://s1.51cto.com/wyfs02/M01/8F/88/wKiom1jkTaWAt1ShAAA8IqDUsGs776.png-wh_500x0-wm_ 3-wmp_4-s_2146149678.png "title=" Qq20170405095041.png "alt=" Wkiom1jktawat1shaaa8iqdusgs776.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://s2.51cto.com/wyfs02/M01/8F/85/wKioL1jkTeaCE-VtAAAwVr4kmB0768.png-wh_500x0-wm_ 3-wmp_4-s_2949916934.png "title=" Qq20170405095146.png "alt=" Wkiol1jkteace-vtaaawvr4kmb0768.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://s3.51cto.com/wyfs02/M02/8F/88/wKiom1jkThmQxBztAAAstaUbBBA781.png-wh_500x0-wm_ 3-wmp_4-s_2581509415.png "title=" Qq20170405095237.png "alt=" Wkiom1jkthmqxbztaaastaubbba781.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://s5.51cto.com/wyfs02/M02/8F/88/wKiom1jkTmmi_cU3AAAqDqpX0b4910.png-wh_500x0-wm_ 3-wmp_4-s_2644524026.png "title=" Qq20170405095357.png "alt=" Wkiom1jktmmi_cu3aaaqdqpx0b4910.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 ();}} /* * one-to-many bidirectional association-fetch= "select", lazy= "false" */@Testpublic void testfetchforselect () {try {session.begintransaction (); Forum forum = session.get (FORUM.CLASS,&NBSP;10); Set<forumpost> forumposts = forum.getforumposts ();for (ForumPost forumPost : forumposts) {system.out.println (Forumpost.getsubject ());} Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}}
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8F/86/wKioL1jkTouTTRZ2AABwCKMO5XE547.png-wh_500x0-wm_ 3-wmp_4-s_2991448208.png "title=" Qq20170324102905.png "alt=" Wkiol1jktouttrz2aabwckmo5xe547.png-wh_50 "/>
This article from the "Vegetarian Yan" blog, declined to reprint!
Hibernate5-1 to multi (1:n)-fetch= "select"-lazy= "false"