Hibernate5-left Outer connection QBC Implementation mode

Source: Internet
Author: User

1. Create project, project name hibernatedemo20, directory structure

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M00/8F/74/wKiom1jfBCDQW-5jAAAgqMiApKE027.png-wh_500x0-wm_ 3-wmp_4-s_2046840566.png "title=" qq20170401093538.png "alt=" wkiom1jfbcdqw-5jaaagqmiapke027.png-wh_50 "/>


2. Create a Lib directory storage jar file in the project, directory structure

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M01/8F/74/wKiom1jfBEPCFepPAABah_00aes310.png-wh_500x0-wm_ 3-wmp_4-s_3927082329.png "title=" qq20170401093616.png "alt=" wkiom1jfbepcfeppaabah_00aes310.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/M02/8F/72/wKioL1jfBHHykm1HAAA4NLsF3_s878.png-wh_500x0-wm_ 3-wmp_4-s_2870340136.png "title=" qq20170401093656.png "alt=" wkiol1jfbhhykm1haaa4nlsf3_s878.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. 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://s4.51cto.com/wyfs02/M02/8F/74/wKiom1jfBLfB7n-nAAA6eoqLopM658.png-wh_500x0-wm_ 3-wmp_4-s_1137672272.png "title=" qq20170401093812.png "alt=" wkiom1jfblfb7n-naaa6eoqlopm658.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://s4.51cto.com/wyfs02/M02/8F/72/wKioL1jfBQ_DIHVaAAA8W5CuMGM310.png-wh_500x0-wm_ 3-wmp_4-s_4249116478.png "title=" qq20170401093937.png "alt=" wkiol1jfbq_dihvaaaa8w5cumgm310.png-wh_50 "/>


8. The contents of the entity class Forumpost are as follows
Package 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://s5.51cto.com/wyfs02/M01/8F/72/wKioL1jfBWbyl2H0AAA8s-dElK8780.png-wh_500x0-wm_ 3-wmp_4-s_2702752666.png "title=" qq20170401094106.png "alt=" wkiol1jfbwbyl2h0aaa8s-delk8780.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://s3.51cto.com/wyfs02/M00/8F/72/wKioL1jfBc6yaM0SAAAwDAZOpHg584.png-wh_500x0-wm_ 3-wmp_4-s_544962007.png "title=" qq20170401094248.png "alt=" wkiol1jfbc6yam0saaawdazophg584.png-wh_50 "/>


12. The contents of the tool class Hbnutil are as follows
Package 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://s5.51cto.com/wyfs02/M01/8F/74/wKiom1jfBgGhUmXdAAArxBVv828301.png-wh_500x0-wm_ 3-wmp_4-s_3878110862.png "title=" qq20170401094342.png "alt=" wkiom1jfbgghumxdaaarxbvv828301.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 structure

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M02/8F/74/wKiom1jfBjizgmb3AAApk-9yXc8454.png-wh_500x0-wm_ 3-wmp_4-s_1824896505.png "title=" qq20170401094436.png "alt=" wkiom1jfbjizgmb3aaapk-9yxc8454.png-wh_50 "/>


The contents of the 16.TESTAPP test class are as follows
package 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-left outer join-qbc */@Testpublic  void testleft () {try { Session.begintransaction (); List<forum> list = session.createcriteria (forum.class). Createcriteria ("forumPosts", jointype.left_outer_join). setresulttransformer (criteria.distinct_root_entity). list ();for  (Forum  Forum : list)  {system.out.println (forum.getname ());} Session.gettransaction (). Commit ();}  catch  (exception e)  {session.gettransaction (). rollback (); e.printstacktrace ();}}


This article from the "vegetarian yan" blog, declined to reprint!

Hibernate5-left Outer connection QBC Implementation mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.