HIBERNATE,HQL language

Source: Internet
Author: User

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.

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.