1.
/** * Query admin, query.list ()*/@Test Public voidEG1 () {Session session=NULL; Try{//Create sessionSession =hibernateutil.getsessionfactory (). Opensession (); //declares a variable and initializesString HQL ="From Admin"; //building a Query objectQuery query =session.createquery (HQL); //Execute QueryList<admin> adminlist =query.list (); //Traverse and output results for(Admin admin:adminlist) {System. out. println ("Account:"+Admin.getloginid ()); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Session! =NULL) {session.close (); } } }
2.
/*** Query Admin,query.iterator ()*/@Test Public voidEG2 () {Session session=NULL; Try{//Create sessionSession =hibernateutil.getsessionfactory (). Opensession (); //declares a variable and initializesString hql = "from Admin"; //building a Query objectQuery query =session.createquery (HQL); //Execute QueryIterator<admin> adminiterator=query.iterate (); //Traverse and output results while(Adminiterator.hasnext ()) {Admin Admin=Adminiterator.next (); System.out.println ("Account:" +Admin.getloginid ()); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Session! =NULL) {session.close (); } } }
3.
/*** Set Placeholder*/@Test Public voidEg3 () {Session session=NULL; Try{//Create sessionSession =hibernateutil.getsessionfactory (). Opensession (); //declares a variable and initializesString hql = "from Admin where loginid=?"; //building a Query objectQuery query =session.createquery (HQL); //assign a value to a placeholderQuery.setstring (0, "admin"); //Execute QueryIterator<admin> adminiterator=query.iterate (); //Traverse and output results while(Adminiterator.hasnext ()) {Admin Admin=Adminiterator.next (); System.out.println ("Account Password:" +admin.getloginpwd ()); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Session! =NULL) {session.close (); } } }
4.
/*** Named parameters*/@Test Public voidEG4 () {Session session=NULL; Try{//Create sessionSession =hibernateutil.getsessionfactory (). Opensession (); //declares a variable and initializesString hql = "from Admin where Loginid=:loginid"; //building a Query objectQuery query =session.createquery (HQL); //Query.setstring ("LoginId", "admin"); //Execute QueryIterator<admin> adminiterator=query.iterate (); //Traverse and output results while(Adminiterator.hasnext ()) {Admin Admin=Adminiterator.next (); System.out.println ("Account Password:" +admin.getloginpwd ()); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Session! =NULL) {session.close (); } } }
5.
/*** Set placeholder, Setparameter*/@Test Public voidEg5_1 () {Session session=NULL; Try{//Create sessionSession =hibernateutil.getsessionfactory (). Opensession (); //declares a variable and initializesString hql = "from Admin where loginid=?"; //building a Query objectQuery query =session.createquery (HQL); //assign a value to a placeholderQuery.setparameter (0, "admin"); //Execute QueryIterator<admin> adminiterator=query.iterate (); //Traverse and output results while(Adminiterator.hasnext ()) {Admin Admin=Adminiterator.next (); System.out.println ("Account Password:" +admin.getloginpwd ()); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Session! =NULL) {session.close (); } } }
6.
/*** Setparameter*/@Test Public voidEg5_2 () {Session session=NULL; Try{//Create sessionSession =hibernateutil.getsessionfactory (). Opensession (); //declares a variable and initializesString hql = "from Admin where Loginid=:loginid"; //building a Query objectQuery query =session.createquery (HQL); //Query.setparameter ("LoginId", "admin"); //Execute QueryIterator<admin> adminiterator=query.iterate (); //Traverse and output results while(Adminiterator.hasnext ()) {Admin Admin=Adminiterator.next (); System.out.println ("Account Password:" +admin.getloginpwd ()); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Session! =NULL) {session.close (); } } }
7.
/*** Dynamically set query parameters*/@Test Public voidEg6 ()throwsexception{Session Session=NULL; Try{//Create sessionSession =hibernateutil.getsessionfactory (). Opensession (); //Prepare query CriteriaAdmin admin=NewAdmin (); Admin.setloginid ("Admin"); //preparing HQL,HQL dynamically generated based on conditionsStringBuilder hql=NewStringBuilder ("from Admin as EMP where 1=1"); if(NULL!=Admin.getclass ()) {Hql.append ("And Loginid=:loginid"); } //building a Query objectQuery query =Session.createquery (hql.tostring ()); //Use the SetProperties () method of the Query object to assign a value to a parameterquery.setproperties (admin); //Execute QueryIterator<admin> adminiterator=query.iterate (); //Traverse and output results while(Adminiterator.hasnext ()) {System.out.println ("Account Password:" +Adminiterator.next (). GETLOGINPWD ()); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Session! =NULL) {session.close (); } } }
8.
/*** Uniqueresult*/ Public voidEg7 () {Session session=NULL; Try{//Create sessionSession =hibernateutil.getsessionfactory (). Opensession (); //declares a variable and initializesString hql = "from Admin where loginid=?"; //building a Query objectQuery query =session.createquery (HQL); Query.setparameter (0, "%ad"); //Execute QueryAdmin admin=(Admin) Query.uniqueresult (); System.out.println ("Account Password:" +admin.getloginpwd ()); } Catch(Exception e) {e.printstacktrace (); } finally { if(Session! =NULL) {session.close (); } } }
9.