1. Create project, project name HIBERNATEDEMO8, directory structure
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/56/wKiom1jbUtOC_XvJAAAuFPz3osI396.png-wh_500x0-wm_ 3-wmp_4-s_2209633937.png "title=" Qq20170329142223.png "alt=" Wkiom1jbutoc_xvjaaaufpz3osi396.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/54/wKioL1jbUvjipkl7AACN0Wp2Aoc477.png-wh_500x0-wm_ 3-wmp_4-s_427508385.png "title=" Qq20170329142302.png "alt=" Wkiol1jbuvjipkl7aacn0wp2aoc477.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://s3.51cto.com/wyfs02/M02/8F/56/wKiom1jbUyygLm0LAABEhsnzq78128.png-wh_500x0-wm_ 3-wmp_4-s_1381791338.png "title=" Qq20170329142353.png "alt=" Wkiom1jbuyyglm0laabehsnzq78128.png-wh_50 "/>
4. Entity Bean Forum content is as follows
Package com.mycompany.demo.bean;public class forum {private int fid;private string name;public forum () {super ();} Public forum (String name) {super (); this.name = name;} 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;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = prime * result + fid;result = prime * result + ((Name == null) ? 0 : name.hashcode ()); return result;} @Overridepublic boolean equals (object obj) {if (this == obj) return true;if (obj == null) return false;if (getclass () != obj.getclass ()) return false; forum other = (Forum) obj;if (Fid != other.fid) return false;if ( Name == null) {if (Other.name != null) Return false;} else if (!name.equals (other.name)) return false;return true;}
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/54/wKioL1jbU2_zAXHGAABCwqXFTpw280.png-wh_500x0-wm_ 3-wmp_4-s_3735782374.png "title=" Qq20170329142501.png "alt=" Wkiol1jbu2_zaxhgaabcwqxftpw280.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 tool class Hbnutil, package name (com.mycompany.demo.util) in the SRC directory,650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/54/wKioL1jbU7Szdh7EAAA_xqBXoiw464.png-wh_500x0-wm_ 3-wmp_4-s_1587686218.png "title=" Qq20170329142611.png "alt=" Wkiol1jbu7szdh7eaaa_xqbxoiw464.png-wh_50 "/>
8. 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 ();}}
9. Create the Hibernate profile Hibernate.cfg.xml in the SRC directory,650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8F/54/wKioL1jbU-vT_fiMAAA7ZTnF4Vo548.png-wh_500x0-wm_ 3-wmp_4-s_1763220577.png "title=" Qq20170329142704.png "alt=" Wkiol1jbu-vt_fimaaa7ztnf4vo548.png-wh_50 "/>
The contents of the 10.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 ">
11. Create the test directory in the project to store testing files, file name Manageforum, package name (Com.mycompany.demo.bean), directory structure650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8F/56/wKiom1jbVCCjXFq8AAA3ZjZYAhw588.png-wh_500x0-wm_ 3-wmp_4-s_1498994919.png "title=" Qq20170329142757.png "alt=" Wkiom1jbvccjxfq8aaa3zjzyahw588.png-wh_50 "/>
The contents of the 12.ManageForum test class are as followspackage com.mycompany.demo.bean;import java.math.biginteger;import java.util.list;import Org.hibernate.hibernateexception;import org.hibernate.query;import org.hibernate.sqlquery;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.criterion.Order; import org.hibernate.criterion.projections;import org.hibernate.criterion.restrictions;import Org.junit.before;import org.junit.test;import com.mycompany.demo.util.hbnutil;public class ManageForum {private Session session; @Beforepublic void init () {session = hbnutil.getsession ();} /* * Uniqueness Query-sql */@Testpublic void testuniquesql () {try {session.begintransaction ( ); string sql = "SELECT&NBSP;*&NBSP;FROM&NBSP;HNSQ_FORUM&NBSP;WHERE&NBSP;FID&NBSP;=&NBSP;?"; forum forum = (forum) session.createsqlquery (SQL). Addentity (Forum.class). SetInteger (0,& nbsp; Uniqueresult (); System.out.println (Forum.getname ()); Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * Uniqueness Query-hql */@Testpublic void testuniquehql () {try {session.begintransaction ( ); string sql = "FROM&NBSP;FORUM&NBSP;WHERE&NBSP;FID&NBSP;=&NBSP;?"; forum forum = (forum) session.createquery (SQL). Setinteger (0, 54). Uniqueresult (); System.out.println (Forum.getname ()); Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * Uniqueness Query-qbc */@Testpublic &NBSP;VOID&NBSP;TESTUNIQUEQBC () {try {session.begintransaction ( ); forum forum = (forum) session.createcriteria (Forum.class). Add (Restrictions.eq ("FID", ). Uniqueresult (); System.out.println (Forum.getname ()); Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * aggregate function Query-sql */@Testpublic void testcountsql () {try {session.begintransaction ( ); string sql = "Select count (*) from hnsq_forum"; biginteger total = (BigInteger) session.createsqlquery (SQL). Uniqueresult (); SYSTEM.OUT.PRINTLN (total); Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * aggregate function Query-hql */@Testpublic void testcounthql () {try {session.begintransaction ( ); string sql = "Select count (*) from forum"; long total = (Long) session.createquery (SQL). Uniqueresult (); SYSTEM.OUT.PRINTLN (total); Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * aggregate function Query-qbc */@Testpublic void&nbsP;TESTCOUNTQBC () {try {session.begintransaction (); string sql = "Select count (*) from forum"; long total = (Long) session.createcriteria (Forum.class). Setprojection (Projections.count (" FID "). Uniqueresult (); SYSTEM.OUT.PRINTLN (total); Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}}
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/56/wKiom1jbVEjALldSAABwCKMO5XE054.png-wh_500x0-wm_ 3-wmp_4-s_2908690346.png "title=" Qq20170324102905.png "alt=" Wkiom1jbvejalldsaabwckmo5xe054.png-wh_50 "/>
This article from the "Vegetarian Yan" blog, declined to reprint!
hibernate5-unique Query and aggregate query