Hibernate conditional query in several ways + query all records

Source: Internet
Author: User

Conditional query

1, the first kind, with? Placeholders, such as://login (with the? placeholder)  PublicList<userpo> Loginuser (Userpo up)throwsexception{Session Session=hibernatesessionfactory.getsession (); String hql= "from Userpo where name =?" and pwd=? "; Query Query=session.createquery (HQL); Query.setstring (0, Up.getname ()); Query.setstring (1, Up.getpwd ()); List<UserPO> list =query.list ();  Session.close (); returnlist;}2, with ": +named "placeholders, such as://Login (with ": Name" placeholder)  PublicList<userpo> LoginUser2 (Userpo up)throwsexception{Session Session=hibernatesessionfactory.getsession (); String hql= "from Userpo where name =: N and pwd=:p"; Query Query=session.createquery (HQL); Query.setstring (N, Up.getname ()); Query.setstring ("P", Up.getpwd ()); List<UserPO> list =query.list ();  Session.close (); returnlist;} 2.1, you can also use this placeholder to set values such as://Login (with ": Name" placeholder, set value with Setparameter)  PublicList<userpo> LoginUser3 (Userpo up)throwsexception{Session Session=hibernatesessionfactory.getsession (); String hql= "from Userpo where name =: N and pwd=:p"; Query Query=session.createquery (HQL); Query.setparameter (N, Up.getname ()); Query.setparameter ("P", Up.getpwd ()); List<UserPO> list =query.list ();  Session.close (); returnlist;} Using this method does not need to state the type of mapping, Hibernate will automatically go through the configuration, but since Hibernate has two date formats: Date and timestamp, you must specify the type of the mapping for the date type. Wording:3, parameter binding according to the object, such as://Login (with ": Name" placeholder, set the value with SetProperties, the name parameter must be the same as the property name being bound)  PublicList<userpo> LoginUser4 (Userpo up)throwsexception{Session Session=hibernatesessionfactory.getsession (); String hql= "from Userpo where name =: Name and pwd=:p WD"; Query Query=session.createquery (HQL);  Query.setproperties (UP); List<UserPO> list =query.list ();  Session.close (); returnlist;}4, use condition query (criteria), such as://Sign In (conditional query criteria) completely out of SQL statements and HQL statements  PublicList<userpo> LoginUser5 (Userpo up)throwsexception{Session Session=hibernatesessionfactory.getsession (); Criteria CRI= Session.createcriteria (Userpo.class); Cri.add (Restrictions.eq ("Name", Up.getname ())); Cri.add (Restrictions.eq ("PWD", Up.getpwd ())); List<UserPO> list =cri.list ();  Session.close (); returnlist;} 5, offline conditional queries, such as://Login (query Detachedcriteria with offline conditions)  PublicList<userpo> LoginUser6 (Userpo up)throwsexception{Session Session=hibernatesessionfactory.getsession (); Detachedcriteria DC= Detachedcriteria.forclass (Userpo.class); Dc.add (Restrictions.eq ("Name", Up.getname ())); Dc.add (Restrictions.eq ("PWD", Up.getpwd ())); Criteria CRI=Dc.getexecutablecriteria (session); List<UserPO> list =cri.list ();  Session.close (); returnlist;} Using offline you can write it in the business layer and pass it in as a parameter to reduce the DAO's code. 6, paged query: The paging query is the processing method in the database application, and the query and Criteria interfaces provide a method for paging queries:1) Setfirstresult (int): Specifies which object to start the query from, and the parameter is the index position, starting at 0. 2) Setmaxresult (int): Specifies the maximum number of objects to query at one time.

Query all records:

/*** Check all the records*/  Public StaticList SelectAll () {List List=NULL; Try{Session=hibernatesessionfactory.getsession (); Transaction Tran=session.begintransaction (); Query Q= Session.createquery ("From User1"); List=q.list ();  Tran.commit (); } Catch(Exception e) {e.printstacktrace (); } finally{hibernatesessionfactory.closesession (); }  returnlist;} 

Hibernate conditional query in several ways + query all records

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.