1. Create project, project name hibernatedemo18, directory structure
650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M02/8F/6D/wKiom1jd7WjwzxB_AAAgper2RGU078.png-wh_500x0-wm_ 3-wmp_4-s_4092924420.png "title=" qq20170331134630.png "alt=" wkiom1jd7wjwzxb_aaagper2rgu078.png-wh_50 "/>
2. Create a Lib directory storage jar file in the project, directory structure
650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M00/8F/6B/wKioL1jd7ZmAHJRpAABa3pasqfg548.png-wh_500x0-wm_ 3-wmp_4-s_188341677.png "title=" qq20170331134706.png "alt=" wkiol1jd7zmahjrpaaba3pasqfg548.png-wh_50 "/>
3. Create the entity Bean Forum, package name (com.mycompany.demo.bean) in the SRC directory,
650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M01/8F/6D/wKiom1jd7bzw7nkDAAA3yme0bHo348.png-wh_500x0-wm_ 3-wmp_4-s_3145630882.png "title=" qq20170331134753.png "alt=" wkiom1jd7bzw7nkdaaa3yme0bho348.png-wh_50 "/>
4. Entity Bean Forum content 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. In the SRC directory, create the Entity Bean Forum mapping file Forum.hbm.xml, package name (com.mycompany.demo.bean),
650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M02/8F/6D/wKiom1jd7fbDmGdbAAA58NkNmj8143.png-wh_500x0-wm_ 3-wmp_4-s_1391263113.png "title=" qq20170331134852.png "alt=" wkiom1jd7fbdmgdbaaa58nknmj8143.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 Bean forumpost, package name (com.mycompany.demo.bean) in the SRC directory,650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M00/8F/6D/wKiom1jd7ivhVVqNAAA73AYfKBw687.png-wh_500x0-wm_ 3-wmp_4-s_940266038.png "title=" qq20170331134946.png "alt=" wkiom1jd7ivhvvqnaaa73ayfkbw687.png-wh_50 "/>
8. The contents of the entity Bean 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 a mapping file for entity Bean Forumpost in the SRC directory ForumPost.hbm.xml, package name (com.mycompany.demo.bean),650) this.width=650; "src=" https://s3.51cto.com/wyfs02/M02/8F/6B/wKioL1jd7lzB0b8aAAA8I0pJBX4413.png-wh_500x0-wm_ 3-wmp_4-s_2977423364.png "title=" qq20170331135034.png "alt=" wkiol1jd7lzb0b8aaaa8i0pjbx4413.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/M02/8F/6E/wKiom1jd7pLATLTqAAAwNOBJV70195.png-wh_500x0-wm_ 3-wmp_4-s_141940602.png "title=" qq20170331135126.png "alt=" wkiom1jd7platltqaaawnobjv70195.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://s4.51cto.com/wyfs02/M00/8F/6E/wKiom1jd7r3jg5iiAAAsMtMkXYc891.png-wh_500x0-wm_ 3-wmp_4-s_3186021898.png "title=" qq20170331135210.png "alt=" wkiom1jd7r3jg5iiaaasmtmkxyc891.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/M01/8F/6E/wKiom1jd7vXw6c2UAAApo2gUxMg244.png-wh_500x0-wm_ 3-wmp_4-s_1741558158.png "title=" qq20170331135307.png "alt=" wkiom1jd7vxw6c2uaaapo2guxmg244.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.session;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 */@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-left outer join-hql */@Testpublic void testleft () {try { Session.begintransaction (); string hsql = "from forum f left outer join f.forumposts"; List<object[]> list = session.createquery (hsql). list ();for (object[] objects : list) {Forum forum = (Forum) objects[0]; Forumpost forumpost = null;if (objects[1] != null) {forumpost = (ForumPost) objects[1]; System.out.println (forum.getname () + " : " + forumpost.getsubject ());}} Session.gettransaction (). Commit ();} catch (exception e) {session.gettransaction (). rollback (); e.printstacktrace ();}}
650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M02/8F/6E/wKiom1jd7yuzbae3AABwCKMO5XE239.png-wh_500x0-wm_ 3-wmp_4-s_2335320043.png "title=" qq20170324102905.png "alt=" wkiom1jd7yuzbae3aabwckmo5xe239.png-wh_50 "/>
This article from the "vegetarian yan" blog, declined to reprint!
Hibernate5-one-to-many bidirectional association-left Outer CONNECTION-HQL